判断指定的成员名是否属于对象(In和hasOwnProperty)
2012-10-31 16:08
453 查看
function Rect(w, h) { Rect.name = "My Rect"; //静态成员属于类, 不会被对象继承; 须冠类名调用 this.width = w; //this 是指实例化以后的对象或调用该函数的对象 this.height = h; xyz = 123; //这只能当个内部变量来使用 } var r = new Rect(); //判断指定的成员名是否属于对象 alert("width" in r); //true alert("height" in r); //true alert("name" in r); //用“In”来查找是深度查找 查找原型链里 是否有这……属性。 而平时小项目常用“hasOwnProperty”来查找(查找自身小范围内是否有此属性,可能更快捷高效。而“in”有特殊需求时使用。) 附以下写法对比。
var Rect = function(){
this.width = 900;
this.height = 600;
this.name = "wang";
var testPorp = "test";
};
var RectBase = function(){
this.width = 500;
this.height = 400;
this.x = 1;
this.y = 2;
};
Rect.prototype = new RectBase();
var rect = new Rect();
console.log("x" in rect);
console.log(rect.hasOwnProperty("x"));
//以下转载
理顺 JavaScript (15) - 类的继承手段: prototype
prototype(原型) 是 JavaScript 中类的继承手段;一个类也不过是一组属性和方法的集合, 所谓继承就是继承属性或方法;
属性是个值, 方法是个函数, JavaScript 喜欢把它们都叫成属性, 我们喜欢把它们叫做成员;
JavaScript 默认让每个函数都拥有一个 prototype 对象, 它可以指向一个对象或函数(函数也是对象, 一回事);
绕来绕去, 最终是四通八达...
类成员与对象成员
function Rect(w, h) { Rect.name = "My Rect"; //静态成员属于类, 不会被对象继承; 须冠类名调用 this.width = w; //this 是指实例化以后的对象或调用该函数的对象 this.height = h; xyz = 123; //这只能当个内部变量来使用 } var r = new Rect(); //判断指定的成员名是否属于对象 alert("width" in r); //true alert("height" in r); //true alert("name" in r); //false //遍历对象成员 for (x in r) { alert(x); //width / height } //遍历类成员 for (x in Rect) { alert(x); //name }
继承
function Point(x, y) { this.x = x; this.y = y; }; function Rect(w, h) { Rect.name = "My Rect"; this.width = w; this.height = h; } Rect.prototype = new Point(); //让 Rect 再从 Point 类继承 var r = new Rect(); //继承以后就可以这样使用 r.x = 1; r.y = 2; r.width = 3; r.heigth = 4; //遍历对象成员 for (x in r) { alert(x); //y / x / width / height } //遍历类成员 for (x in Rect) { alert(x); //name } //建立方法 constructor 属于 prototype, 既然 Rect 是继承与 Point, 那么: alert(r.constructor); /* 将显示: function Point(x, y) { this.x = x; this.y = y; }; */
hasOwnProperty、propertyIsEnumerable、isPrototypeOf
function Point(x, y) { this.x = x; this.y = y; }; function Rect(w, h) { Rect.name = "My Rect"; this.width = w; this.height = h; } Rect.prototype = new Point(); var r = new Rect(); /* 可用 hasOwnProperty 方法判断指定成员是否是对象的固有成员(而非继承来的) */ alert(r.hasOwnProperty('x')); //false alert(r.hasOwnProperty('y')); //false alert(r.hasOwnProperty('width')); //true alert(r.hasOwnProperty('height')); //true /* 但不能用 hasOwnProperty 判断成员是否是继承来的, 譬如 */ alert(r.hasOwnProperty('ABCDEFG')); //false /* propertyIsEnumerable */ alert(r.propertyIsEnumerable('x')); //false alert(r.propertyIsEnumerable('y')); //false alert(r.propertyIsEnumerable('width')); //true alert(r.propertyIsEnumerable('height')); //true /* isPrototypeOf: 是不是参数(参数是个对象)的原型对象 */ alert(Point.prototype.isPrototypeOf(r)); //true alert(Rect.prototype.isPrototypeOf(r)); //true alert(r.isPrototypeOf(r)); //true var obj = {}; alert(Object.prototype.isPrototypeOf(obj)); //true var str = new String(); alert(String.prototype.isPrototypeOf(str)); //true alert(Object.prototype.isPrototypeOf(str)); //true
相关文章推荐
- OC基础day5-点语法、property、编译器指令、id万能指针、类与类的判断、判断对象是否响应指定的方法、判断类中是否有这个方法、参数方法与响应、构造方法、synthesize
- javascript下判断一个对象是否具有指定名称的属性的的代码
- 用in判断input中的placeholder属性是否在这个对象里
- jQuery判断指定对象的class是否存在
- 判断指定对象的类型中是否具有指定的IBA属性
- JS,判断对象是否具有指定的方法?
- JS中isPrototypeOf 和hasOwnProperty 的区别 ------- js使用in和hasOwnProperty获取对象属性的区别
- C#实现java instanceof (判断对象是否属于哪个类)
- 详谈js使用in和hasOwnProperty获取对象属性的区别
- 判断Set集合是否包含指定对象
- 丶使用is关键字判断对象是否与指定类型兼容
- javascript下判断一个对象是否具有指定名称的属性的的代码
- Map.containsKey方法——判断Map集合对象中是否包含指定的键名
- C++类的成员函数存储方式(是否属于类的对象)
- JavaScript中hasOwnProperty函数方法是返回一个布尔值,指出一个对象是否具有指定名称的属性。
- Map.containsKey/Map.containsValue方法——判断Map集合对象中是否包含指定的键名
- jQuery.isEmptyObject()函数用于判断指定参数是否是一个空对象。
- struts2if标签判断对象值是否等于指定值
- 类的成员函数存储方式(是否属于类的对象)
- 通过已知接口对象判断其是否为指定的CLSID类型