在IE8中使用javascript __proto__ [英] Using javascript __proto__ in IE8
问题描述
嗨我在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.
无论如何,这是指定[[原型]的一个小例子]来自现有的对象。请注意,[
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屋!