Javascript 以正确的方式使用Object.create()

Javascript 以正确的方式使用Object.create(),javascript,ecmascript-5,ecmascript-6,Javascript,Ecmascript 5,Ecmascript 6,通过学习Javascript,我找到了创建对象的不同方法。似乎前进的方向是使用Object.create() 对于使用Object.create()的最佳实践,很难找到一个可靠的答案,因为即使是特定的Object.create()文章似乎做的事情也略有不同 我想做的是创建多个具有自己封装数据的对象 我喜欢使用封装,对我来说似乎有效的是 function Foo() { var message = "Hello"; return { bar:bar }

通过学习Javascript,我找到了创建对象的不同方法。似乎前进的方向是使用
Object.create()

对于使用
Object.create()
的最佳实践,很难找到一个可靠的答案,因为即使是特定的
Object.create()
文章似乎做的事情也略有不同

我想做的是创建多个具有自己封装数据的对象

我喜欢使用封装,对我来说似乎有效的是

function Foo() {
    var message = "Hello";

    return {
        bar:bar
    }

    function bar(){ 
        return message; 
    }
}

World = (function(){ 
    var obj = Foo();
    var tank = Object.create(obj);

    return {
        baz:baz
    }

    function baz(){ 
        alert(tank.bar()); 
    }

})();
运行
World.baz()


非常感谢您的回答。

通常在javascript中,您希望创建如下对象:

var obj = {};
obj.someProperty = 'someValue';
obj.someOtherProperty = 'someOtherValue';
或者,您可以使用对象文字表示法,如下所示:

var obj = {
    someProperty: 'someValue',
    someOtherProperty: 'someOtherValue'
};
var Tank = function(speed, durability){
    this.speed = speed;
    this.durability = durability;
    this.location = 0;
    this.shoot = function(){
        console.log('Pew pew');
    };
    this.move = function(){
        this.location += speed;
    };
}

var myTank = new Tank(5, 15);    // creates new tank with speed 5 and durability 15,
                                 // that also has all the default properties and methods,
                                 // like location, shoot, and move.

var yourTank = new Tank(7, 12);  // instantiates a different tank that myTank, with it's
                                 // own speed and durability properties, but also has the
                                 // default location, shoot, and move properties/ methods

var enemyTank = new Tank(10, 25);// instantiates yet another, unique tank with it's own 
                                 // unique values for speed and durability, but again with
                                 // the default location, shoot, and move properties/methods
Object.create函数是一个有趣的函数。是的,它确实创建了一个空对象,但与上面定义的对象不同。实例化和object with object.create将为新的空对象继承赋予object.create函数所赋予的参数。例如,如果我们将对象定义为:

var actions = {
    shout: function(message){
        console.log(message.toUpperCase() + '!');
    }
}
然后使用object.create()创建一个新对象:

newObject将不包含它自己的任何属性,但它将能够访问父操作对象的属性。定义这些对象后,请尝试以下操作:

newObject.hasOwnProperty('shout');    // returns false
newObject.shout('Hello!');    // logs 'HELLO!!'
这个例子只是展示了从新创建的对象到其父对象的继承是如何工作的。这可能非常有用,但在使用Object.create创建对象之前,请确保您特别想要该行为-否则,请确保安全并使用上述两种其他方法之一

希望有帮助

编辑:

或者,如果您只是尝试创建同一对象的多个单独实例,则可以创建一个构造函数并使用new关键字调用它,如下所示:

var obj = {
    someProperty: 'someValue',
    someOtherProperty: 'someOtherValue'
};
var Tank = function(speed, durability){
    this.speed = speed;
    this.durability = durability;
    this.location = 0;
    this.shoot = function(){
        console.log('Pew pew');
    };
    this.move = function(){
        this.location += speed;
    };
}

var myTank = new Tank(5, 15);    // creates new tank with speed 5 and durability 15,
                                 // that also has all the default properties and methods,
                                 // like location, shoot, and move.

var yourTank = new Tank(7, 12);  // instantiates a different tank that myTank, with it's
                                 // own speed and durability properties, but also has the
                                 // default location, shoot, and move properties/ methods

var enemyTank = new Tank(10, 25);// instantiates yet another, unique tank with it's own 
                                 // unique values for speed and durability, but again with
                                 // the default location, shoot, and move properties/methods

我建议使用构造函数来封装数据。如果确实需要使用
Object.create()
,则需要使用
Object.create()
创建构造函数原型系统。但是,在任何情况下,您都只是在调用
World
.baz()
方法中的
Foo()
的结果。这并不意味着
World
应该指向
Foo()
的结果


尝试这种方法来创建封装数据的javaScript对象。正如您所看到的,Foo的每个实例都有自己的属性和状态

    var Foo = function() {

        var Foo = function Foo(customMessage) {
            this.message = customMessage || "Hello";
        }

        Foo.prototype.message;

        Foo.prototype.bar = function(){ 
            return this.message; 
        }

        return Foo;
    }();

    var tank1 = new Foo();
    var tank2 = new Foo("Goodbye");

    alert(tank1.bar());
    alert(tank2.bar());

Foo()
已经返回了一个对象,为什么要使用
object.create
?如果我想创建多个对象,并基于
Foo()
返回的对象创建它们自己的封装数据库?是的,是射击游戏的特定坦克。使用Object.create(Foo)似乎不起作用(一开始似乎在逻辑上应该如此)。当我调用World.baz()时,我得到了未定义的函数。我敢问tank={};,有什么问题吗;?我不能从obj外部访问对象成员“someProperty”吗?“someProperty”位于obj对象上。您可以使用点符号从对象外部访问它:obj.someProperty。对于不是函数的对象的属性,它们存在于该对象上并没有多大区别。但是,在函数的情况下,函数所在的对象经常会对函数的行为产生影响,特别是由于关键字“this”。Noble我最初使用的是new。我被劝阻不要使用new,并且被告知要使用Object.create(),这不像new那么直观,它也给我带来了对象没有自己的局部变量的问题,这是我到达时需要跨越的一座桥梁,你需要基于
Object.create()
创建一个构造函数系统。我编辑了我的答案以显示这一点。@Aaron谁告诉你不要使用新的?这里可以找到一些关于如何以及为什么使用构造函数、prototype和Object.create的示例。