为什么Object.getPrototypeOf()和constructor.prototype记录不同的值

时间:2015-12-29 06:32:54

标签: javascript prototype

在下面的代码中,使用getPrototypeOf()和constructor.prototype给出了不同的值。

function C(){ }
function D(){ }

C.prototype.fname = "John";  

console.log(Object.getPrototypeOf(C)); //function () {}
console.log(C.prototype); // C{fname: "John"}

3 个答案:

答案 0 :(得分:2)

这是因为Object.prototype.isPrototypeOf()返回从其父构造函数继承的原型。

function C(){ }

现在这是由原始类型Function

构建的

因此,当您在getPrototypeOf C上调用__Proto__时,会从继承的父构造函数中返回(阅读此description)。

请参阅下面的插图:请参阅内联评论以了解

typeof C // "function"
C instanceof Function // True
Object.getPrototypeOf(C) === Function.constructor.prototype // "True"
Function.constructor.prototype // "function(){}"
Object.getPrototypeOf(C) // "function(){}"

现在是下一部分:

C.prototype.fname = "John";
typeof C.prototype // object

因此,它的构造函数变为C(),其原型对象链继承自C(),当您创建此类new C()

这样的实例时,它将被继承

<强>因此:

C.prototype.constructor // "function C(){}" -- Parent constructor

因此,当您在Prototype上查找C()时,它会返回从其构造函数继承的prototype,如下所示

C.prototype.constructor === C //true
C.prototype === C.prototype.constructor.prototype // True
C.prototype.constructor.prototype // C {fname: "John"}
C.prototype // C {fname: "John"}

<强>结论:

当您在Object或Function上搜索原型时,会在从其父构造函数继承的属性上进行查找。

Object.getPrototypeOf(C) === C.constructor.prototype //true
Object.getPrototypeOf(C) === Function.prototype // true
C.prototype === (new C()).__proto__ // true (this is for illustration only dont use __proto__ in your code)
  

Object.getPrototypeOf(C)不等于C.prototype因为他们的public class CallSMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { Log.d("SIM_SLOT"," Slot Number "+capturedSimSlot(extras)); } } /*below methods captures the sim slot number from bundle */ public int capturedSimSlot(Bundle bundle){ int whichSIM =-1; if (bundle.containsKey("subscription")) { whichSIM = bundle.getInt("subscription"); } if(whichSIM >=0 && whichSIM < 5){ /*In some device Subscription id is return as subscriber id*/ sim = ""+whichSIM; }else{ if (bundle.containsKey("simId")) { whichSIM = bundle.getInt("simId"); }else if (bundle.containsKey("com.android.phone.extra.slot")) { whichSIM = bundle.getInt("com.android.phone.extra.slot"); }else{ String keyName = ""; for(String key : bundle.keySet()){ if(key.contains("sim")) keyName =key; } if (bundle.containsKey(keyName)) { whichSIM = bundle.getInt(keyName); } } } return whichSIM; } }   构造者是不同的。

<强> 参考文献:

答案 1 :(得分:0)

console.log(Object.getPrototypeOf(C)); 

这将返回C类的原型而不是C的实例。这些应该返回相同的结果:

console.log(Object.getPrototypeOf(new C()));
console.log(C.prototype);

答案 2 :(得分:0)

Object.getPrototypeOf(C);基本上与C.__proto__做同样的事情,就是它

  

“指向实例化对象时用作原型的对象。”

实例化C()时,它是一个函数。看看这个:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

来自MDN的Object.prototype的部分描述:

  

“JavaScript中的所有对象都来自Object;所有对象都从Object.prototype继承方法和属性,尽管它们可能被覆盖(除了具有null原型的Object,即Object.create(null))。”

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype

含义C.prototype只是指向包含从其实例化的对象将继承的方法和属性的对象(或者在这种情况下它被覆盖为字符串),而Object.getPrototypeOf(C)正在查看用于创建C又名C.__proto__的初始对象。