如何在javascript中访问对象原型?

发布于 2024-12-08 16:41:39 字数 483 浏览 0 评论 0原文

所有文章都写到 JavaScript 是一种基于原型的语言,这意味着每个对象都有一个原型(或者更准确地说,原型链)。

到目前为止,我已经尝试了以下代码片段:

var F = function();
F.prototype.member1 = 1;
var object1 = new F();
console.log(object1.member1); // prints 1

如何访问 object1 的原型对象?有没有一种与浏览器无关的方法来做到这一点(我的意思是,不依赖 __proto__ 属性?参见 这个链接,但也许自 2010 年以来有新的发展)如果我不能,你能分享一下背后的原理吗?

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).

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 this link, but maybe there are new developments since 2010) If I can't, could you share please the rationale behind the hood?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

好久不见√ 2024-12-15 16:41:39
var f = function();
var instance = new f();

如果您知道实例的名称​​类函数,您可以简单地访问原型:

var prototype = f.prototype;
prototype.someMember = someValue;

如果您不知道:

1)

var prototype = Object.getPrototypeOf(instance);
prototype.someMember = someValue;

2)

var prototype = instance.__proto__;
prototype.someMember = someValue;

3) 或者

var prototype = instance.constructor.prototype; // works only if constructor is properly assigned and not modified
prototype.someMember = someValue;

为了兼容性,您可以将以下代码片段放入代码中(并始终使用 Object.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;

    };

  }
}

更新:

根据 ECMA-262第 6 版(2015 年 6 月)__proto__ 属性已标准化为 Web 浏览器的附加功能。现在所有最新版本的顶级浏览器都支持它。了解有关 __proto__ 的更多信息:

var f = function();
var instance = new f();

If you know name of instance class function, you can simply access prototype as:

var prototype = f.prototype;
prototype.someMember = someValue;

If you don't:

1)

var prototype = Object.getPrototypeOf(instance);
prototype.someMember = someValue;

2) or

var prototype = instance.__proto__;
prototype.someMember = someValue;

3) or

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):

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__:

南街九尾狐 2024-12-15 16:41:39

它看起来

Object.getPrototypeOf(passedObject);

适用于此,并且与现代浏览器兼容。

以下是 MDN 上的兼容性表

It looks like

Object.getPrototypeOf(passedObject);

will work for this, and is compatible with modern browsers.

Here are the compatibility tables on MDN

野の 2024-12-15 16:41:39
var F = function(){};
var object1 = new F();
alert(object1.constructor === F);
alert(object1.constructor.prototype === F.prototype);
var F = function(){};
var object1 = new F();
alert(object1.constructor === F);
alert(object1.constructor.prototype === F.prototype);
断念 2024-12-15 16:41:39
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
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
画骨成沙 2024-12-15 16:41:39
var F = function();
F.prototype.member1 = 1;
var intance = new F();
console.log(instance['member1']);
var F = function();
F.prototype.member1 = 1;
var intance = new F();
console.log(instance['member1']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文