var str ="";alert("str.hasOwnProperty("split")的结果是:"+str.hasOwnProperty("split")); //return falsealert("String.prototype.hasOwnProperty("split")的结果是:"+String.prototype.hasOwnProperty("split"));//return true运行结果:

hasOwnProperty的用法不仅仅在此,在Jquery中在编写插件中,少不了的一步,就是初始化参数,其中一个很重要的方法就是$.extend();他的原理就是应用了hasOwnProperty()方法;利用for in 循环遍历对象成员中,有没有相同名称的对象成员,有的话就用这个新的对象成员替换掉旧的,通过这种方式,我们就可以通过修改方法中的参数变化,从而控制程序的流程,而对于那些没有改变的部分,仍使用默认值进行控制,我们自己也可以简单的模拟一下这个extend函数,如下
function extend(target,source){//target 旧的 source新的 for (var i in source){if(target.hasOwnProperty(i)){target[i]=source[i];}}return target;}var a={"first":,"second":"lyl","third":"bob"};var b={"third":"leo"};extend(a,b);for(var i in a){alert(a[i]);//原本是bob,现在变成leo了} 2.isPrototypeOf(object)<script type="text/javascript">function foo(){this.name = "foo";}function bar(){}bar.prototype = new foo();var goo = new bar();alert(goo.name); //fooalert(bar.prototype.isPrototypeOf(goo));//true,在bar的原型链中有当前对象goo,则isPrototypeOf方法返回true</script> 3.propertyIsEnumerable(propertyName)var p = new Object(); //通过Object直接创建对象//为p对象动态添加属性p.Age=;p.Name="孤傲苍狼";//扩展Object类,为Object类添加一个Show方法Object.prototype.Show=function(){alert(this.Age+" "+this.Name);}alert(p.Age);p.Show();document.write("<pre>");document.writeln("p.constructor:"+p.constructor);//得到对象的构造函数document.writeln("Object.prototype:"+Object.prototype);//得到prototype对象,prototype是静态属性,只能通过"类名.prototype"去访问document.writeln("p.isPrototypeOf(p):"+p.isPrototypeOf(p));document.writeln("p.hasOwnProperty("Age"):"+p.hasOwnProperty("Age"));document.writeln("p.propertyIsEnumerable("Age"):"+p.propertyIsEnumerable("Age"));document.writeln("p.toString():"+p.toString());document.writeln("p.valueOf():"+p.valueOf());document.write("</pre>"); 运行结果:
测试代码2:
var Car = function(){};Car.prototype.hello = function(){alert("hello car");};var car = new Car();car.f = function() {alert("自定义方法");}document.write("<pre>");document.writeln("car.hasOwnProperty("f")的结果是:"+car.hasOwnProperty("f"));//ture,car对象有f方法document.writeln("car.propertyIsEnumerable("f")的结果是:"+car.propertyIsEnumerable("f"));//ture,car对象有f方法,f方法是可以被枚举的document.writeln("car.hasOwnProperty("hello")"+car.hasOwnProperty("hello")); // false,因为car本身没有hello方法document.writeln("car.propertyIsEnumerable("hello")的结果是:"+car.propertyIsEnumerable("hello")); // false,没有这个方法当然不能枚举document.writeln("car.constructor.prototype.hasOwnProperty("hello")的结果是:"+car.constructor.prototype.hasOwnProperty("hello"));// true,car的类Car的原型有hello方法document.writeln("car.constructor.prototype.propertyIsEnumerable("hello")的结果是:"+car.constructor.prototype.propertyIsEnumerable("hello"));// true, car的类的Car的原型hello方法是可以被枚举的document.writeln("Car.prototype.hasOwnProperty("hello")的结果是:"+Car.prototype.hasOwnProperty("hello"));// true,car的类Car的原型有hello方法document.writeln("Car.prototype.propertyIsEnumerable("hello")的结果是:"+Car.prototype.propertyIsEnumerable("hello"));document.write("</pre>"); 运行结果: 
以上所述是小编给大家介绍的JavaScript知识点总结(十一)之js中的Object类详解,希望对大家有所帮助