(js高手之路)原型对象(原型)与原型链相关属性与方法详解

  

<强>一运算符:

<强>运算符 <>强检测左侧的__proto__原型链上,是否存在右侧的原型原型。我在之前的两篇文章

(js高手之路)构造函数的基本特性与优缺点

(js高手之路)一步步图解javascript的原型(原型)对象,原型链

function  CreateObj (uName), {   ,,,,,,,,,,,this.userName =, uName;   ,,,,,,,,,,,this.showUserName =, function  (), {   ,,,,,,,,,,,,,,,return “100”;   ,,,,,,,,,,,}   ,,,,,,,}   ,,,,,,,CreateObj.prototype.showUserName =, function  (), {   ,,,,,,,,,,,return  this.userName;   ,,,,,,,}   ,,,,,,,var  obj1 =, new  CreateObj (“ghostwu”);   ,,,,,,,var  obj2 =, new  CreateObj(“卫庄”);   ,,,,,,,console.log (instanceof , obj1  CreateObj );//真实   ,,,,,,,console.log (instanceof , obj2  CreateObj );//真实   ,,,,,,,console.log (instanceof , obj1  Object );//真实   ,,,,,,,console.log (instanceof , obj2  Object );,//真正的


如果隐式原型__proto__指向调用isPrototypeOf()方法的对象原型(CreateObj),那么这个方法就返回正确的,如:

 1,,,,,,,,, var  obj1 =, new  CreateObj (“ghostwu”);
  2,,,,,,,,,var  obj2 =, new  CreateObj(“卫庄”);
  3,,,,,,,,,console.log (, CreateObj.prototype.isPrototypeOf (, obj1 ),),,//真实的
  4,,,,,,,,,console.log (, CreateObj.prototype.isPrototypeOf (, obj2 ),),,//真正的

因为其中obj1, methoda的隐式原型__proto__指向的都是CreateObj。原型,有朋友可能会问CreateObj。原型上面根本就没有isPrototypeOf这个方法,怎么可以

调用呢?

是的,没的错,但是CreateObj.prototype的隐式原型__proto__指向了对象。原型,而isPrototypeOf存在对象。原型上,所以就能够调用

三、对象。getPrototypeOf

获取实例的隐式原型(__proto__)的指向,因为其中obj1, methoda的__proto__都指向CreateObj。原型

 function  CreateObj (uName), {
  ,,,,,,,,,,,this.userName =, uName;
  ,,,,,,,}
  ,,,,,,,CreateObj.prototype.showUserName =, function  (), {
  ,,,,,,,,,,,return  this.userName;
  ,,,,,,,}
  ,,,,,,,CreateObj.prototype.age =, 22;
  ,,,,,,,var  obj1 =, new  CreateObj (“ghostwu”);
  ,,,,,,,obj1.age =, 20;
  ,,,,,,,var  obj2 =, new  CreateObj(“卫庄”);
  ,,,,,,,console.log (, obj1.age );,//20——→来自实例
  ,,,,,,,console.log (, obj2.age );,//22——→来自原型对象
  ,,,,,,,delete  obj1.age;
  ,,,,,,,console.log (, obj1.age );,//22——→来自原型

五,hasOwnProperty

判断属性是实例上的还是原型对象上的,如果是实例上的,返回真的,原型上的返回假

function  CreateObj (uName), {   ,,,,,,,,,,,this.userName =, uName;   ,,,,,,,}   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

(js高手之路)原型对象(原型)与原型链相关属性与方法详解