Javascript 如何在javascript中访问对象原型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7662147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access object prototype in javascript?
提问by BreakPhreak
In all the articles it is written that JavaScript is a prototype-based language, meaning that every object has a prototype (or, more precisely, prototype chain).
在所有文章中,JavaScript 是一种基于原型的语言,这意味着每个对象都有一个原型(或者更准确地说,原型链)。
So far, I've tried the following code snippet:
到目前为止,我已经尝试了以下代码片段:
var F = function();
F.prototype.member1 = 1;
var object1 = new F();
console.log(object1.member1); // prints 1
How can I access the prototype object of object1
? Is there a browser-neutral way to do that (I mean, not relying on __proto__
property? Seen thislink, but maybe there are new developments since 2010) If I can't, could you share please the rationale behind the hood?
如何访问 的原型对象object1
?有没有一种浏览器中立的方式来做到这一点(我的意思是,不依赖__proto__
财产?看到这个链接,但也许自 2010 年以来有新的发展)如果我不能,你能分享一下引擎盖背后的基本原理吗?
回答by Andrew D.
var f = function();
var instance = new f();
If you know name of instance
class function, you can simply access prototype as:
如果您知道instance
类函数的名称,您可以简单地访问原型为:
var prototype = f.prototype;
prototype.someMember = someValue;
If you don't:
如果你没有:
1)
1)
var prototype = Object.getPrototypeOf(instance);
prototype.someMember = someValue;
2)or
2)或
var prototype = instance.__proto__;
prototype.someMember = someValue;
3)or
3)或
var prototype = instance.constructor.prototype; // works only if constructor is properly assigned and not modified
prototype.someMember = someValue;
For compatibility you can place into your code the following snippet (and use always Object.getPrototypeOf(instance)
to return prototype):
为了兼容性,您可以将以下代码段放入您的代码中(并使用 alwaysObject.getPrototypeOf(instance)
返回原型):
if(!Object.getPrototypeOf) {
if(({}).__proto__ === Object.prototype && ([]).__proto__ === Array.prototype) {
Object.getPrototypeOf = function getPrototypeOf(object) {
return object.__proto__;
};
} else {
Object.getPrototypeOf = function getPrototypeOf(object) {
// May break if the constructor has been changed or removed
return object.constructor ? object.constructor.prototype : void 0;
};
}
}
UPDATE:
更新:
According to ECMA-262 6th Edition (June 2015) __proto__
property is standardized as additional feature for Web browsers. All latest editions of top browsers supports it now. Read more about __proto__
:
根据 ECMA-262 第 6 版(2015 年 6 月),__proto__
属性被标准化为 Web 浏览器的附加功能。现在所有最新版本的顶级浏览器都支持它。阅读更多关于__proto__
:
EDMA-262 6th Edition (June 2015):
B.2.2.1 Object.prototype.__proto__
EDMA-262 第 6 版(2015 年 6 月):
B.2.2.1 Object.prototype.__proto__
回答by HelloWorld
It looks like
看起来像
Object.getPrototypeOf(passedObject);
will work for this, and is compatible with modern browsers.
将为此工作,并与现代浏览器兼容。
Here are the compatibility tables on MDN
回答by HelloWorld
var F = function(){};
var object1 = new F();
alert(object1.constructor === F);
alert(object1.constructor.prototype === F.prototype);
回答by r3mark
var F = function();
F.prototype.member1 = 1;
F.prototype.getClass = F;
var object1 = new F();
object1.member1 = 2;
console.log(object1.getClass.prototype.member1); // prints 1
console.log(object1.member1); // prints 2
回答by Kado
var F = function();
F.prototype.member1 = 1;
var intance = new F();
console.log(instance['member1']);