`
xiaobo.liu
  • 浏览: 38990 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

javascript 要知道的

阅读更多
<script  type="text/javascript">
$(document).ready(function(){
   //javascript 的封装  匿名自调用函数
   (function(){
         var tax = 0.5, price = 'drting';      
   })()
   //alert(typeof tax);
   //这样在函数外不能访问函数内的变量  模块化编程
   var g = (function(){
         var tax = 0.5, price = 'drting';  
		 return {iprice : price,
		         add :function(){}
		 
		 };
   })();
   /*
	   alert(g.iprice);

	   alert('0' == '');  //false
	   alert('' == '0');  //false

	   alert(false == '0');  //true
	   alert('\t\r\n ' == 0); // true

	   浏览器中for-in遍历对象属性和方法时会包括对象原型链上的所有属性和方法。
	   但绝大多数属性是不希望被枚举出来的。
	   可以用hasOwnProperties方法来检测属性是否属于对象
   */
     function Dog (name) 
	 {
         this.name = name;
     }

     Dog.prototype.legs = 4;
     Dog.prototype.speak = function () 
	 {
         return "woof!";
     };
     
     var d = new Dog("Bowser");
     
     for (var prop in d)
	 {
         console.log( prop + ": " + d[prop] );
     }
     
     console.log("=====");
     
     for (var prop in d) {
      if (d.hasOwnProperty(prop)) 
		 {
			  console.log( prop + ": " + d[prop] );
		 }
     }
     
     // Output
     // name: Bowser
     // legs: 4
     // speak: function () {
     // return "woof!";
     // }
     // =====
     // name: Bowser

    //有时,只希望枚举列出对象的的属性,不包括方法。可以用typeof方法,代码如下:

    for (var prop in d) 
	{
		 if (typeof d[prop] !== 'function') 
		 {
		      console.log( prop + ": " + d[prop] );
		 }
    }

   /*Document fragments 是一个DOM元素容器,可以使用它同时添加这些元素到页面中。
   Document fragment自身不是一个DOM节点,它不会在页面DOM树中显示,
   并且在把它插入DOM之前它是不可见的。下面是它的用法:
   */
     var list = document.getElementById("list"),
         frag = document.createDocumentFragment(),
         items = ["one", "two", "three", "four"],
         el;
     
     for (var i = 0; items[i]; i++)
	 {
		 el = document.createElement("li");
		 el.appendChild( document.createTextNode(items[i]) );
		 frag.appendChild(el); // better!
     }
     
     list.appendChild(frag);
});
</script>
<div id="list"></div>
</body> 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics