Object.getPrototypeOf 和 example.isPrototypeOf(obj) 给出了令人困惑的结
Object.getPrototypeOf and example.isPrototypeOf(obj) gave confusing results
我读到Object.gePrototypeOf(someObject)
返回传递对象的原型,如果aPrototype
是someObject
的原型,则aPrototype.isPrototypeOf(someObject)
返回true。对我来说很明显,如果Object.getPrototypeOf(someObject)
返回一个名为 aPrototype
的原型,那么aPrototype.isPrototypeOf(someObject)
将返回 true。但它在我的代码中没有发生:
function person(fname, lname)
{
this.fname = fname;
this.lname = lname;
}
var arun = new person('Arun', 'Jaiswal');
console.log(Object.getPrototypeOf(arun)); //person
console.log(person.isPrototypeOf(arun)); //false
怎么了?
根据 MDN,isPrototype
的语法是
prototypeObj.isPrototypeOf(obj)
另请参阅 isPrototypeOf vs instanceof
function person(fname, lname)
{
this.fname = fname;
this.lname = lname;
}
var arun = new person('Arun', 'Jaiswal');
console.log(Object.getPrototypeOf(arun)); //person
console.log(person.prototype.isPrototypeOf(arun));
arun
的原型不是person
而是person.prototype
:
Object.getPrototypeOf(arun) === person.prototype; // true
person.prototype.isPrototypeOf(arun); // true
isPrototypeOf
不是在构造函数本身上调用,而是在构造函数的原型属性上调用。
alert(person.prototype.isPrototypeOf(arun)); // true
这意味着阿伦的原型不是person
,而是person.prototype
。
相关文章:
- 使用$(el).addClass('example');但是不要't增加负载
- 使用 JavaScript Regex 将“First.Last@example.com”变成“flast”
- 在第'www.foo.com',可以从'www.example.com'将ajax请求发
- 我得到了这个错误,但是不要;我不知道如何修复它,www.example.com/undefined?1460018627
- 只有当输入文本是example.com这样的域时才执行操作
- AngularJS $http.get example
- Example of <iframe src= "view-source:http://www.abc.
- 除了 http://example.com 之外,如何扩展此正则表达式以匹配 www.example.com
- Javascript Select Box Style Example (no Javascript framework
- Ext JS 4 ItemSelector example
- “if (location.href != example.com/page1 || example.com/page2
- 节点 JS -URL 参数像 example.com/profile/me 一样简化
- Object.getPrototypeOf 和 example.isPrototypeOf(obj) 给出了令人困惑的结
- 如何将 url (http://example.com) 的一个实例限制为仅在客户端打开一次
- Javascript Closure: Pratical Closures example in MDN
- WebGL Multiple Canvas three.js example
- 访问控制允许源不允许 http://example.com
- 脚本 src=//example.com/file.js 是否有效
- Meteor Flow路由器设置example.com/singlePostPage
- XMLHttpRequest无法加载http://example.com/myapp/jsonurl/.否'访问