File indexing completed on 2024-04-28 15:28:38

0001 // Object.prototype.hasOwnProperty
0002 
0003 function MyClass()
0004 {
0005   this.y = 2;
0006 }
0007 
0008 MyClass.prototype = { x : 1 };
0009 
0010 var sub = new MyClass();
0011 shouldBe("sub.x","1");
0012 shouldBe("sub.y","2");
0013 shouldBe("sub.hasOwnProperty('x')","false");
0014 shouldBe("sub.hasOwnProperty('y')","true");
0015 sub.x = 6;
0016 shouldBe("sub.x","6");
0017 shouldBe("sub.hasOwnProperty('x')","true");
0018 delete sub.y;
0019 shouldBe("sub.y","undefined");
0020 shouldBe("sub.hasOwnProperty('y')","false");
0021 
0022 
0023 
0024 // Object.prototype.isPrototypeOf
0025 
0026 function Class1() {}
0027 function Class2() {}
0028 function Class3() {}
0029 
0030 Class1.prototype = new Object();
0031 Class1.prototype.hasClass1 = true;
0032 Class2.prototype = new Class1();
0033 Class2.prototype.hasClass2 = true;
0034 Class3.prototype = new Class2();
0035 Class3.prototype.hasClass3 = true;
0036 
0037 var obj = new Class3();
0038 shouldBe("obj.hasClass1","true");
0039 shouldBe("obj.hasClass2","true");
0040 shouldBe("obj.hasClass3","true");
0041 
0042 shouldBe("Class1.prototype.isPrototypeOf(obj)","true");
0043 shouldBe("Class2.prototype.isPrototypeOf(obj)","true");
0044 shouldBe("Class3.prototype.isPrototypeOf(obj)","true");
0045 shouldBe("obj.isPrototypeOf(Class1.prototype)","false");
0046 shouldBe("obj.isPrototypeOf(Class2.prototype)","false");
0047 shouldBe("obj.isPrototypeOf(Class3.prototype)","false");
0048 
0049 shouldBe("Class1.prototype.isPrototypeOf(Class2.prototype)","true");
0050 shouldBe("Class2.prototype.isPrototypeOf(Class1.prototype)","false");
0051 shouldBe("Class1.prototype.isPrototypeOf(Class3.prototype)","true");
0052 shouldBe("Class3.prototype.isPrototypeOf(Class1.prototype)","false");
0053 shouldBe("Class2.prototype.isPrototypeOf(Class3.prototype)","true");
0054 shouldBe("Class3.prototype.isPrototypeOf(Class2.prototype)","false");
0055 
0056 shouldBeUndefined("Class1.prototype.prototype");
0057 
0058 function checkEnumerable(obj,property)
0059 {
0060   for (var propname in obj) {
0061     if (propname == property)
0062       return true;
0063   }
0064   return false;
0065 }
0066 
0067 function myfunc(a,b,c)
0068 {
0069 }
0070 myfunc.someproperty = 4;
0071 
0072 shouldBe("myfunc.length","3");
0073 shouldBe("myfunc.someproperty","4");
0074 shouldBe("myfunc.propertyIsEnumerable('length')","false");
0075 shouldBe("myfunc.propertyIsEnumerable('someproperty')","true");
0076 shouldBe("checkEnumerable(myfunc,'length')","false");
0077 shouldBe("checkEnumerable(myfunc,'someproperty')","true");