Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中创建新的空对象_Javascript_Object - Fatal编程技术网

在JavaScript中创建新的空对象

在JavaScript中创建新的空对象,javascript,object,Javascript,Object,当试图创建一个新对象时,一个空的对象用于操作,我无法取出旧数据 以下是我尝试过的一个例子: function Foo() { this.Bar = Bar; // etc.. } var Bar = { __words : {}, addWord : function (word, amount) { this.__words[word] = amount; } // etc.. } 现在,当我创建一个新对象时: var foo

当试图创建一个新对象时,一个空的对象用于操作,我无法取出旧数据

以下是我尝试过的一个例子:

function Foo() {
    this.Bar = Bar;
    // etc..
}

var Bar = {
    __words : {},
    addWord : function (word, amount) {
        this.__words[word] = amount;
    }
    // etc..
}
现在,当我创建一个新对象时:

var foo = new Foo();
var bar = foo.Bar;
bar.addWord("hello",7);
bar.addWord("world",9);

var lorem = new Foo();
var words = lorem.Bar.__words; // This will display {hello:7,world:9} from the
                               // previous object
我还尝试使用
Object.create()
,但仍然是一样的,显示了前一个对象中的
\u单词

我无法取出旧数据

您没有清除实际上是全局的
var-Bar
,这意味着您正在使用
var-lorem=new-Foo()创建一个新对象
this.Bar = Bar; 
如果你想要一个新的对象,你应该给Foo它自己的Bar对象。 或包括:

__words : {},
    addWord : function (word, amount) {
        this.__words[word] = amount;
在富本身

不管怎样,如果您只需要这一次,您可以简单地清空Bar中的单词数组

我无法取出旧数据

您没有清除实际上是全局的
var-Bar
,这意味着您正在使用
var-lorem=new-Foo()创建一个新对象
this.Bar = Bar; 
如果你想要一个新的对象,你应该给Foo它自己的Bar对象。 或包括:

__words : {},
    addWord : function (word, amount) {
        this.__words[word] = amount;
在富本身


不管怎样,如果您只需要一次,您可以简单地清空Bar中的单词数组。

代码中有几个问题

function Foo() {
    this.Bar = Bar;
    // etc..
}

var Bar = {
    __words : {},
    addWord : function (word, amount) {
        this.__words[word] = amount;
    }
    // etc..
}
Bar应该在Foo之前定义,但它可能不会出现问题,因为变量实际上是在函数范围级别定义的,在这个特定示例中,这可能不是问题

也就是说,您正在Foo构造函数中复制一个对象。对象是可变的,因此无法工作。对对象的一个更改将更改另一个。你能做的就是这样。这里有另一种方法,不用新的

var Bar = {
    create: function () {
       var obj = Object.create(this);
       obj.__words= {};
       return obj;
    },

    addWord: function (word, amount) {
       this.__words[word] = amount;
    }
    ...other methods...
};

var Foo = function () {
    this.Bar = Bar.create();
    // etc..
};
但我不知道如何在其他浏览器中支持对象。这是相当于Felix写的语法,您可以清楚地删除两个对象的需要

唯一的区别是,不编写新的Bar()。实际上,您正在创建一个原型对象,并从原型中创建一个对象。create是构造函数

this.Bar = Bar; 
Foo可以用同样的方式写,你会得到这样的结果

var Bar = {
    create: function () {
       var obj = Object.create(this);
       obj.__words= {};
       return obj;
    },

    addWord: function (word, amount) {
       this.__words[word] = amount;
    },
    ...other methods...
};

var Foo = {
    create: function () {
      var foo = Object.create(this);
      foo.Bar = Bar.create();
      return foo;
    },
    ....
};

var foo1 = Foo.create();
var foo2 = Foo.create();

foo1.bar.addWord("allo", 3);
我想这完全取决于你想做什么,但是create方法比“new”操作符有一些优势。例如,您可以创建异步方法,在对象创建之后执行回调

这里有更多的信息。最后,情况基本相同


代码中有几个问题

function Foo() {
    this.Bar = Bar;
    // etc..
}

var Bar = {
    __words : {},
    addWord : function (word, amount) {
        this.__words[word] = amount;
    }
    // etc..
}
Bar应该在Foo之前定义,但它可能不会出现问题,因为变量实际上是在函数范围级别定义的,在这个特定示例中,这可能不是问题

也就是说,您正在Foo构造函数中复制一个对象。对象是可变的,因此无法工作。对对象的一个更改将更改另一个。你能做的就是这样。这里有另一种方法,不用新的

var Bar = {
    create: function () {
       var obj = Object.create(this);
       obj.__words= {};
       return obj;
    },

    addWord: function (word, amount) {
       this.__words[word] = amount;
    }
    ...other methods...
};

var Foo = function () {
    this.Bar = Bar.create();
    // etc..
};
但我不知道如何在其他浏览器中支持对象。这是相当于Felix写的语法,您可以清楚地删除两个对象的需要

唯一的区别是,不编写新的Bar()。实际上,您正在创建一个原型对象,并从原型中创建一个对象。create是构造函数

this.Bar = Bar; 
Foo可以用同样的方式写,你会得到这样的结果

var Bar = {
    create: function () {
       var obj = Object.create(this);
       obj.__words= {};
       return obj;
    },

    addWord: function (word, amount) {
       this.__words[word] = amount;
    },
    ...other methods...
};

var Foo = {
    create: function () {
      var foo = Object.create(this);
      foo.Bar = Bar.create();
      return foo;
    },
    ....
};

var foo1 = Foo.create();
var foo2 = Foo.create();

foo1.bar.addWord("allo", 3);
我想这完全取决于你想做什么,但是create方法比“new”操作符有一些优势。例如,您可以创建异步方法,在对象创建之后执行回调

这里有更多的信息。最后,情况基本相同


每个
Foo
实例之间共享
Bar
引用的对象

我真的看不出把逻辑放进两个对象有什么意义,你只需要一个构造函数就可以做到:

function Foo() {
    this.__words = {};
}

Foo.prototype.addWord = function(word, amount) {
    this.__words[word] = amount;
}


var foo = new Foo();
foo.addWord("hello",7);

var bar = new Foo();
bar.addWord("world",9);
如果确实需要分离功能,那么也可以将
Bar
作为构造函数,并在
Foo
构造函数方法中创建一个新实例:

function Foo() {
    this.bar = new Bar();
}


function Bar() {
    this.__words = {};
}

Bar.prototype.addWord = function(word, amount) {
    this.__words[word] = amount;
}

Bar
引用的对象在每个
Foo
实例之间共享

我真的看不出把逻辑放进两个对象有什么意义,你只需要一个构造函数就可以做到:

function Foo() {
    this.__words = {};
}

Foo.prototype.addWord = function(word, amount) {
    this.__words[word] = amount;
}


var foo = new Foo();
foo.addWord("hello",7);

var bar = new Foo();
bar.addWord("world",9);
如果确实需要分离功能,那么也可以将
Bar
作为构造函数,并在
Foo
构造函数方法中创建一个新实例:

function Foo() {
    this.bar = new Bar();
}


function Bar() {
    this.__words = {};
}

Bar.prototype.addWord = function(word, amount) {
    this.__words[word] = amount;
}

有控制台日志吗?我的猜测是,
var Bar
应该在类定义对象通过引用传递之前,您必须为每个新实例克隆它,比如
this.Bar=clone(Bar)
@elclanrs:您还需要编写
clone()
Bar
是单个对象。您想创建多个对象。@SLaks:嗯,是的,请看这里有控制台日志吗?我的猜测是,
var Bar
应该在类定义对象通过引用传递之前,您必须为每个新实例克隆它,比如
this.Bar=clone(Bar)
@elclanrs:您还需要编写
clone()
Bar
是单个对象。你想创建多个对象。@SLaks:嗯,是的,请看这里,你的解决方案行不通。
Bar.\uu words
中的对象将由您使用
object.create(Bar)
创建的所有实例共享。你只是把问题移到了一个层次(可以这么说)。你的解决方案行不通。
Bar.\uu words
中的对象将由您使用
object.create(Bar)
创建的所有实例共享。你只是把问题移到了一个层次(可以这么说)。