标签: object-create

如何使用 Crockfords 的 Object.create() (Javascript) 访问我祖先的重写方法

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}
var o1 = {};
o1.init = function(){
   alert('o1');
};
var o2 = Object.create(o1);
o2.init = function(){
   // how would I call my ancessors init()?
   alert('o2');
};
o2.init();
Run Code Online (Sandbox Code Playgroud)

javascript inheritance prototype object-create

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

使用Object.create而不是new时传递参数

这个问题与使用"Object.create"而不是"new"不重复.有问题的线程并不专注于在使用时正确传递参数Object.create


我很好奇我将如何使用Object.create而不是初始化对象new.到目前为止,这是我的代码:

function Human(eyes) {
    this.eyes = eyes || false;
}

Human.prototype.hasEyes = function() {
    return this.eyes;
}

function Male(name) {
    this.name = name || "No name";
}

Male.prototype = new Human(true); //passing true to the Human constructor

var Sethen = new Male("Sethen");

console.log(Sethen.hasEyes());
Run Code Online (Sandbox Code Playgroud)

如上所示,Male.prototype = new Human(true);使用true创建一个新对象.hasEyes()运行该函数时,会按预期记录true.

所以,我的问题是..使用Object.create我将如何以同样的方式传递true参数?

javascript object-create

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

javascript 构造函数和object.create 可以结合使用吗?

更新

如果这是不可能的,请随时提供解释原因的答案。我很乐意将其标记为已接受。


我想稍微简化以下代码(对象“声明”的两个步骤,我想要一个):

var Masher = function(opts) {
    this._name  = opts.name;
};

Masher.prototype = Object.create(Object.prototype, {
    _name:  { writable: true },
    name:  { get: function() { return this._name;  }}
});

// Note: (new Masher({name: 'bar'})).name == 'bar'
Run Code Online (Sandbox Code Playgroud)

我想一次性创建整个函数原型,构造函数出现在 Object.create 的某处。也许,像这样:

var Basher = Object.create(Function.prototype, {
    _name:  { writable: true },
    name:  { get: function() { return this._name;  }},
    constructor: { value: function(opts) { this._name = opts.name; }}
});
Run Code Online (Sandbox Code Playgroud)

但是,当我调用时new Basher(),我得到:'TypeError: object is not a function'。

虽然我意识到我可以用语法糖(一个帮助程序库)来做到这一点,但我的目标是让事情尽可能简单,并对 JS 对象、原型、构造函数内部结构有一些了解。我试图阅读尽可能多的内容: …

javascript constructor new-operator object-create

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

Javascript Object.create无法在Firefox中运行

我总是在Firefox(3.6.14)中得到以下异常:

TypeError: Object.create is not a function
Run Code Online (Sandbox Code Playgroud)

这非常令人困惑,因为我非常确定它是一个功能,并且代码在Chrome上按预期工作.

负责此行为的代码行如下:

Object.create( Hand ).init( cardArr );
Object.create( Card ).init( value, suit );
Run Code Online (Sandbox Code Playgroud)

如果有人想看到所有代码,它来自扑克图书馆gaga.js:https://github.com/SlexAxton/gaga.js

也许有人知道如何让它在Firefox中运行?

javascript object-create

4
推荐指数
1
解决办法
3281
查看次数

使用Object.Create向JavaScript对象添加函数

我正在尝试在JavaScript中使用Object.Create.我目前有以下代码:

var vehicle = {
        getModel: function () {
        console.log( "The model of this vehicle is.." + this.model );
    }
};

var car = Object.create(vehicle, {
    "id": {
       value: 9,         
    },
    "model": {
       value: "Ford",  
    }
});

var van = Object.create(vehicle, {
  "id": {
     value: 10,    
   },
    "model": {
      value: "Big Van",
      enumerable: true
   },
    "make": {
      value: "Warrior",      
   },
    "getMake": function () {
       console.log( "The make of this vehicle is.." + this.make );
   }
});
Run Code Online (Sandbox Code Playgroud)

我试图为getMake添加一个函数,但是我得到了错误:

TypeError:当我调用时,对象#的属性'getMake'不是函数:

van.getMake();

这可能吗?你怎么做呢?

javascript oop object-create

4
推荐指数
1
解决办法
952
查看次数

在Java中创建对象时避免'新'Keword的好处

我找到了一些博客,其中提出了new在创建类的对象时避免使用关键字的建议.创建没有new关键字的对象的一些示例是-

SampleObject obj = Class.forName("com.example.SampleObject").newInstance();
Run Code Online (Sandbox Code Playgroud)

或使用clone()方法 -

SampleObject obj1 = new SampleObject();  
SampleObject obj2 = obj.clone();
Run Code Online (Sandbox Code Playgroud)

然后在这里我找到了一些没有new关键字创建对象的好例子

我可以理解'Factory'模式的优点,同时避免new代码主要部分的关键字.如果我没有使用任何设计模式(用于创建对象),那么创建没有new关键字的对象有什么好处?或者没有创建对象的原因是什么new

java oop instantiation instance object-create

4
推荐指数
1
解决办法
3566
查看次数

从哪个版本,IE可以支持Object.create(null)?

您可以通过多种方式在JavaScript中创建对象:

// creates an object which makes the Object, prototype of data.
var data1 = new Object(); 

// Object literal notation; Object still is the prototype of data2.
var data2 = {}; 

// anotherObject is now the prototype of data3.
var data3 = Object.create(anotherObject); 

/* data3 is an object which can be verified bye typeof operator, 
   however, it now has no prototype and you can 
   build everything from scratch. */
var data3 = Object.create(null); 
Run Code Online (Sandbox Code Playgroud)

但我不知道哪个版本的IE支持最后一个方法,即Object.create(null)方法?

javascript javascript-objects object-create

3
推荐指数
1
解决办法
5683
查看次数

访问使用Object.create创建的JavaScript属性的get/set方法

我想要的是能够包装JavaScript属性以修改get/set上的行为.

对于值的属性,我可以执行以下操作:

var obj = {
    myProperty : 0
};

function notifyOfChange(obj, propertyName) {
    var propertyValue = obj[propertyName];
    Object.defineProperty(obj, propertyName, {
        get : function() { return propertyValue; },
        set : function(newValue) {
            var propertyValue = newValue;
            console.log("Message from notifyOfChange."); 
        }
    });
};

obj.myProperty = 10; // outputs "Message from notifyOfChange."
Run Code Online (Sandbox Code Playgroud)

但是,如果myProperty已经有一个getter/setter怎么办?

var obj = Object.create({}, {
    myProperty : {
        get : function() { return this._myProperty; },
        set : function(value) {
            console.log("Message from obj itself.");
            this._myProperty = value;
        }, …
Run Code Online (Sandbox Code Playgroud)

javascript defineproperty object-create

3
推荐指数
1
解决办法
3055
查看次数

Object.create和Prototypes

当我使用object.create创建一个像这样的新对象时,

o = {x:1,y:2};
p = Object.create(o);
Run Code Online (Sandbox Code Playgroud)

我的印象是o成为p的原型并继承了它的所有方法.

那么,为什么,当我尝试

print(p.prototype);
Run Code Online (Sandbox Code Playgroud)

输出未定义?o定义明确!!

谢谢

javascript prototype object-create

3
推荐指数
1
解决办法
210
查看次数

NodeJS中的Object.create

与FireFox相比,Object.create在Nodejs中的工作方式不同.

假设一个像这样的对象:

objDef = {
  prop1: "Property 1"
}

obj = {
  prop2: "Property 2"
}

var testObj = Object.create(obj, objDef);
Run Code Online (Sandbox Code Playgroud)

上面的javascript在Mozilla中运行得很好.它基本上使用传递给Object.create的第二个参数来设置默认值.

但这在Node中不起作用.我得到的错误是TypeError: Property description must be an object: true.

如何在Node中使用它?我想基本上创建一个具有默认值的对象.

node.js object-create

3
推荐指数
2
解决办法
9001
查看次数

Object.create和继承

以下示例中的结果对象之间有什么区别:

var EventEmitter = require('events').EventEmitter;

var oProto  = Object.create(EventEmitter.prototype);
var oProto2 = Object.create(oProto);

var oConstr  = Object.create(new EventEmitter);
var oConstr2 = Object.create(oConstr);
Run Code Online (Sandbox Code Playgroud)

我想oConstr并且oConstr2将在EventEmitter构造函数中设置任何属性,但是还有其他有意义的区别吗?

javascript inheritance node.js ecmascript-5 object-create

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

Java对象创建的区别

我是Java中的OOP概念的新手.这两起事件有什么区别?

1.

ClassName obj_name = new ClassName();
obj_name.method();
Run Code Online (Sandbox Code Playgroud)

2.

new ClassName().method();
Run Code Online (Sandbox Code Playgroud)

非常感谢一个很好的解释.谢谢

java oop object-create

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