与此问题类似但不同.以下代码来自JavaScript:The Definitive Guide.他基本上定义了一个继承方法,如果它存在则遵循Object.create,否则使用构造函数和交换原型进行普通的旧Javascript继承.
我的问题是,由于Object.create在很多常见的 IE 浏览器中都不存在,甚至试图使用它有什么意义呢?它肯定会使代码混乱,上一个问题的一位评论者提到Object.create 并不是太快.
那么尝试添加额外代码以便偶尔使用这个ECMA 5函数有什么优势呢?这个函数可能会或者可能不会比这种"旧"方式慢?
function inherit(p) {
if (Object.create) // If Object.create() is defined...
return Object.create(p); // then just use it.
function f() {}; // Define a dummy constructor function.
f.prototype = p; // Set its prototype property to p.
return new f(); // Use f() to create an "heir" of p.
}
Run Code Online (Sandbox Code Playgroud) 我一直在使用EcmaScript 5规范中的Object.create,我正在尝试创建一个多继承类型结构.
假设我有一些功能:a,b和c.只处理原型,我可以这样做:
function a () {}
a.prototype = {
fnA = function () {},
propA = 500
};
function b () {}
b.prototype = a.prototype;
b.prototype.fnB = function () {};
b.prototype.propB = 300;
function c () {}
c.prototype = b.prototype;
c.prototype.fnC = function () {};
c.prototype.propC = 200;
Run Code Online (Sandbox Code Playgroud)
但是使用Object.create,我会做一些事情:
function a() {}
a.prototype = {
fnA = function () {},
propA = 500
};
var b = Object.create(new a());
b.fnB = function () {};
b.propB = 300;
var c …Run Code Online (Sandbox Code Playgroud) javascript prototypal-inheritance ecmascript-5 object-create
new到目前为止,我一直在使用JavaScript中的关键字.我一直在阅读Object.create,我想知道是否应该使用它.我不太明白的是我经常需要运行构造代码,因此我看不到Object.create它将如何工作,因为它不会触发任何运行的函数.
谁能告诉我,在哪种情况下我应该使用Object.create而不是new?
它看起来像的Object.create创建磁盘阵列走路像数组嘎嘎像数组,但仍然不是真正的数组.至少用v8/node.js.
> a = []
[]
> b = Object.create(Array.prototype)
{}
> a.constructor
[Function: Array]
> b.constructor
[Function: Array]
> a.__proto__
[]
> b.__proto__
[]
> a instanceof Array
true
> b instanceof Array
true
> Object.prototype.toString.call(a)
'[object Array]'
> Object.prototype.toString.call(b)
'[object Object]'
Run Code Online (Sandbox Code Playgroud)
一些Javascript专家可以解释为什么会这样,以及如何使它使我新创建的数组与真正的数组无法区分?
我的目标是克隆数据结构,包括可能附加自定义属性的数组.当然,我可以使用手动将属性附加到新创建的数组Object.defineProperty,但有没有办法使用Object.create?
我是从Python和Smalltalk的背景来看Javascript,我很欣赏Self和Lisp在语言中的用法.使用ECMAScript5,我想在没有新操作符的情况下尝试使用原型OO.
约束:
以下是我尝试实施以满足标准:
function subclass(Class, Base) {
"use strict";
function create(self, args) {
if (!(self instanceof this))
self = Object.create(this.prototype);
var init = self.__init__;
return init ? init.apply(self, args) : self;
}
if (Base instanceof Function) Base = Base.prototype;
else if (Base===undefined) Base = Object.prototype;
Class.prototype = Object.create(Base);
Class.prototype.constructor = Class;
Class.create = create;
Class.define = function define(name, fn) { return Class.prototype[name] = fn; };
Class.define('__name__', Class.name);
return Class;
}
Run Code Online (Sandbox Code Playgroud)
它似乎在一个简单的模型中工作:
function Family(){return Family.create(this, arguments)}
subclass(Family, …Run Code Online (Sandbox Code Playgroud) 今天我在玩的时候注意到Chrome控制台中的一些对象被显示为Object而不是构造函数名称.
这很奇怪,所以我把它归结为以下代码:
function Baz() {
this.baz = true;
}
var b = new Baz();
var c = Object.create(b);
console.log(b); // why is b outputting with Object not Baz?
Run Code Online (Sandbox Code Playgroud)
在上面的代码中b,不是通过a创建的Object.create,但是在记录时它表示Object.我没有错字,错误地询问c.当我甚至没有碰到那个物体时,b的记录已被改变.创建另一个实例c,不应该改变b.
这必须是Chrome错误吗?反正有没有让Chrome在Baz这里正确报道?
这对于调试目的很重要.

更新错误提交:https://code.google.com/p/chromium/issues/detail? id = 478522
我是原型继承的新手,所以我试图理解'正确'的方式.我以为我能做到这一点:
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var tbase = {};
tbase.Tdata = function Tdata() {};
tbase.Tdata.prototype.say = function (data) {
console.log(data);
};
var tData = new tbase.Tdata();
tbase.BicData = Object.create(tData);
tbase.BicData.prototype.say = function (data) {
console.log("overridden: " + data)
};
tbase.BicData.prototype.shout = function (data, temp) {
console.log("SHOUT: " + data + ", " + temp)
};
var test = new tbase.BicData();
tData.say("test1");
test.say("test2"); …Run Code Online (Sandbox Code Playgroud) 作为javascript的初学者,我试图从这里了解Object.create()方法
https://developer-new.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
在示例代码中,第18行.创建一个accessable属性,其writeable设置为true.我还读到可写只适用于数据描述符.
试过跑,
var o = Object.create(Object.prototype, {
// foo is a regular "value property"
foo: {
writable:true, configurable:true, value: "hello"
},
// bar is a getter-and-setter (accessor) property
bar: {
writable: true,
configurable: false,
get: function() { return 10 },
set: function(value) { console.log("Setting `o.bar` to", value) }
}
});
console.log(o);
Run Code Online (Sandbox Code Playgroud)
我得到invalid property error.
假设我有一个这样的对象:
var Foo = {
x: 5,
sprite: new Image()
}
Run Code Online (Sandbox Code Playgroud)
问题:我想用正确的src初始化该sprite.但是,当我使用以下创建技术时:
var f = Object.create(Foo);
Run Code Online (Sandbox Code Playgroud)
我没有构造方法(也就是init函数)来设置 sprite.src = 'cool.png';
我的问题:
如果我正在使用对象文字技术,并且Object.create(),我何时实际初始化我的一些内部状态(如示例new Image())
我的解决方案
var Foo = {
create: function() {
var f = Object.create(Foo);
f.sprite.src = 'cool.png';
return f;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不知道这是否是一个很好的模式.如果有办法,我想做"JavaScript方式".:)
谢谢!
在此代码中,原型仍然可以更改.
我怎样才能阻止原型的改变?
var a = {a:1}
var b={b:1}
var c = Object.create(a)
Object.getPrototypeOf(c) //a
c.__proto__ = b;
Object.getPrototypeOf(c) //b
var d = Object.create(null)
Object.getPrototypeOf(d) //null
d.__proto__ = b;
Object.getPrototypeOf(d) //null
Run Code Online (Sandbox Code Playgroud) javascript ×10
object-create ×10
ecmascript-5 ×3
prototype ×2
console.log ×1
inheritance ×1
mozilla ×1
oop ×1
v8 ×1