标签: object-create

使用Object.create的优点

此问题类似但不同.以下代码来自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)

javascript javascript-objects ecmascript-5 object-create

10
推荐指数
1
解决办法
1736
查看次数

Object.create与直接原型继承

我一直在使用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

9
推荐指数
1
解决办法
1139
查看次数

有没有理由在JavaScript中使用Object.create()或new?

new到目前为止,我一直在使用JavaScript中的关键字.我一直在阅读Object.create,我想知道是否应该使用它.我不太明白的是我经常需要运行构造代码,因此我看不到Object.create它将如何工作,因为它不会触发任何运行的函数.

谁能告诉我,在哪种情况下我应该使用Object.create而不是new

javascript ecmascript-5 object-create

8
推荐指数
2
解决办法
4821
查看次数

使用Object.create创建的Javascript数组 - 不是真正的数组?

它看起来像的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

javascript v8 object-create

8
推荐指数
1
解决办法
2726
查看次数

使用Object.create和命名构造函数的Prototypal OO

我是从Python和Smalltalk的背景来看Javascript,我很欣赏Self和Lisp在语言中的用法.使用ECMAScript5,我想在没有新操作符的情况下尝试使用原型OO.

约束:

  • 可选的new运算符来创建类
  • 原型链必须是正确的
  • 用于WebInspector调试支持的命名构造函数
  • alloc().init()创建序列,如Objective-C和Python

以下是我尝试实施以满足标准:

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)

javascript oop prototype object-create

7
推荐指数
1
解决办法
1905
查看次数

Object.create改变Chrome中proto对象的控制台输出?

今天我在玩的时候注意到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

javascript google-chrome object-create console.log

7
推荐指数
1
解决办法
189
查看次数

原型继承:你可以链接Object.create吗?

我是原型继承的新手,所以我试图理解'正确'的方式.我以为我能做到这一点:

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 inheritance prototype object-create

6
推荐指数
1
解决办法
1715
查看次数

javascript中的Object.create方法

作为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.

javascript mozilla object-create

6
推荐指数
2
解决办法
1820
查看次数

如何使用ES5 Object.create和对象文字语法模拟构造函数?

假设我有一个这样的对象:

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方式".:)

谢谢!

javascript object-create

5
推荐指数
2
解决办法
8626
查看次数

如何防止改变原型?

在此代码中,原型仍然可以更改.

我怎样才能阻止原型的改变?

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 object-create

5
推荐指数
1
解决办法
401
查看次数