javaScript对象知识点

 1 //-----------------prototype属性的使用-----------------------------------
 2 //javaScript为每一个类型(type)提供了一个prototype属性,
 3 //将这个属性指向一个对象,这个对象就成为了这个类型的原型;
 4 //这就意味着这个类型所创建的所有对象都有这个原型的特性。
 5 function ClassA(){}
 6 
 7 function ClassB(){}
 8 
 9 ClassB.prototype = new ClassA();    //ClassB以ClassA的对象为原型
10 
11 function ClassC(){}
12 
13 ClassC.prototype = new ClassB();
14 
15 var obj = new ClassC();
16 
17 document.write(obj instanceof ClassC);   //true
18 document.write(obj instanceof ClassB);   //true
19 document.write(obj instanceof ClassA);    //true
20 document.write(obj instanceof Object);   //true

1 //----------toLocaleString()--方法的使用---------------------------
2 var now = new Date();
3 document.write(now.toString()+"<br />");
4 document.write(now.toLocaleString());  //toLocaleString()返回对象本地化的字符串表示
5 
6 
7 结果:
8 Thu Jan 01 2015 21:08:41 GMT+0800
9 2015/1/1 下午9:08:41 


 1 //---------hasOwnProperty()----方法的使用------------------------
 2 function dwn(s){
 3     document.write(s+"<br />");
 4 }
 5 
 6 function ClassA(){
 7     this.a = 1;
 8 }
 9 
10 function ClassB(){
11     this.b = 5;
12 }
13 
14 ClassB.prototype = new ClassA();      //管理对象继承的机制,可以返回对象类型原型的引用
15 var objB = new ClassB();
16 
17 dwn(objB);
18 dwn("a" in objB);
19 dwn("b" in objB);
20 dwn("a是对象B的原型继承来的属性吗? "+objB.hasOwnProperty("a"));
21 dwn("b是对象B的原型继承来的属性吗? "+objB.hasOwnProperty("b"));
22 
23 
24 结果:
25 [object Object]
26 true
27 true
28 a是对象B的原型继承来的属性吗? false
29 b是对象B的原型继承来的属性吗? true
原文地址:https://www.cnblogs.com/yzdqxing/p/4197827.html