在IE8中使用javascript __proto__ [英] Using javascript __proto__ in IE8

查看:194
本文介绍了在IE8中使用javascript __proto__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我在javascript中有这两个对象

Hi I have this two objects in javascript

var john = { firstname: 'John', lastname: 'Smith' }

var jane = { firstname: 'Jane' }

这样做:

jane.__proto__ = john;

我可以访问jane的房产以及john的房产

I can access to jane's properties and also john's properties

如果IE8中不支持 __ proto __ ,那么相当于写这个:

If __proto__ is not supported in IE8 for example, what's the equivalent to write this:

jane.__proto__ = john;

谢谢!

推荐答案

IE中没有等效或标准的机制。 ( __ proto __ 属性是非标准扩展名,因为ECMAScript标准中未指定 。)

There is no equivalent or standard mechanism in IE. (The __proto__ property in Firefox is non-standard extension as is not specified in the ECMAScript standards.)

[[prototype]]对象只能通过在函数对象上设置 prototype 属性来指定 ,该对象充当构造函数之前构建一个新对象。然而,[[prototype]]可以稍后突变

The [[prototype]] object can only be specified by setting the prototype property upon the function-object which acts as the constructor before the construction of a new object. The [[prototype]] can, however, be mutated later.

无论如何,这是指定[[原型]的一个小例子]来自现有的对象。请注意,[创建新对象之前完成。 ECMAScript第5版介绍 Object.create ,它可以执行以下操作>和浅层克隆一个对象。

Anyway, here is a little example of specifying a [[prototype]] from an existing object. Note that the [[prototype]] assignment must be done before the new object is created. ECMAScript 5th edition introduces Object.create which can do the following and shallow-clone an object.

function create (proto) {
    function f () {}
    f.prototype = proto
    return new f
}
var joe = create({})
var jane = create(joe)
joe.name = "joe"                   // modifies object used as jane's [[prototype]]
jane.constructor.prototype === joe // true
jane.__proto__ === joe             // true  -- in Firefox, but not IE
jane.name                          // "joe" -- through [[prototype]]
jane.constructor.prototype = {}    // does NOT re-assign jane's [[prototype]]
jane.name                          // "joe" -- see above

这篇关于在IE8中使用javascript __proto__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆