Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.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 使用foreach内部对象创建对象_Javascript_Object - Fatal编程技术网

Javascript 使用foreach内部对象创建对象

Javascript 使用foreach内部对象创建对象,javascript,object,Javascript,Object,有一种“选项”方法,我将一个对象作为参数发送,并希望该对象存储在变量answer中。代码是: var Selectable = { create: function(type) { Object.create(this); this.type = type; this.id = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 5); this

有一种“选项”方法,我将一个对象作为参数发送,并希望该对象存储在变量
answer
中。代码是:

var Selectable = {
    create: function(type) {
        Object.create(this);
        this.type = type;
        this.id =  Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 5);

        this.cacheDom();
        return this;
    },
    cacheDom: function(){
        this.$target = $('#selectable-target');
        this.$id = $('#selectable-' + this.id);
        this.$options = this.$id.find('.selectable-options');
        this.$revise = this.$id.find('a.revise');
    },
    options: function(values){
        this.answers = {};
        Object.keys(values).forEach(function(key) {
            this.answers[key] = values[key];
        });
    },
    render: function() {
        this.$target.append(
            $('<div>')
            .attr('id', 'selectable-'+this.id)
            .append(
                $('<div>')
                .addClass('selectable-options')
            )
        )

        this.cacheDom();
    }
};
要解决此问题,我可以将对象复制到属性,如下所示:

options: function(values){
            this.answers = values;
}
var selectable = new Selectable();
selectable.options({'foo':'bar'});
我想知道为什么会发生这种情况,以及如何修复它。

错误在

Object.keys(values).forEach(function(key) {
  this.answers[key] = values[key];
});
如果查看,您将看到,在使用
forEach
时,
在回调中是
未定义的
,除非您传递自定义参数。解决方案是将其重写如下:

Object.keys(values).forEach(function(key) {
  this.answers[key] = values[key];
}, this);
更改将强制回调中的值
this
等于调用者中的值
this

等效地,但仅限于ES6,您可以使用
=>
而不是
函数,该函数捕获
,并且在我上次检查时(至少是1年前)要快一点:

错误就在这里

Object.keys(values).forEach(function(key) {
  this.answers[key] = values[key];
});
如果查看,您将看到,在使用
forEach
时,
在回调中是
未定义的
,除非您传递自定义参数。解决方案是将其重写如下:

Object.keys(values).forEach(function(key) {
  this.answers[key] = values[key];
}, this);
更改将强制回调中的值
this
等于调用者中的值
this

等效地,但仅限于ES6,您可以使用
=>
而不是
函数,该函数捕获
,并且在我上次检查时(至少是1年前)要快一点:


首先,您必须了解什么是对象以及哪个“this”属于哪个实例。在以下代码中:

1 options: function(values){ // outer function
2     this.answers = {};
3     Object.keys(values).forEach(function(key) { // inner function
4         this.answers[key] = values[key];
5     });
6 },
第一个“this”(第二行)-链接到外部函数实例。但第二个“这个”——与内在功能有关。更重要的是,“这个”中没有一个没有链接到可选的

要解决这样的混乱,首先你必须把对象作为一个函数引入

var Selectable = function() { 
而不是将“this”保存在某个变量中

var that = this;
而不是在所有内部函数中使用“that”而不是“this”

最后,使用如下可选选项:

options: function(values){
            this.answers = values;
}
var selectable = new Selectable();
selectable.options({'foo':'bar'});

希望它能有所帮助

首先,你必须了解什么是对象,以及哪个“this”属于哪个实例。在以下代码中:

1 options: function(values){ // outer function
2     this.answers = {};
3     Object.keys(values).forEach(function(key) { // inner function
4         this.answers[key] = values[key];
5     });
6 },
第一个“this”(第二行)-链接到外部函数实例。但第二个“这个”——与内在功能有关。更重要的是,“这个”中没有一个没有链接到可选的

要解决这样的混乱,首先你必须把对象作为一个函数引入

var Selectable = function() { 
而不是将“this”保存在某个变量中

var that = this;
而不是在所有内部函数中使用“that”而不是“this”

最后,使用如下可选选项:

options: function(values){
            this.answers = values;
}
var selectable = new Selectable();
selectable.options({'foo':'bar'});

希望有帮助

如果调用函数时没有
new
操作符(或
apply
call
方法),则对象
将是全局窗口对象。这就是调用
Selectable.create()
@jcbp:No.
Selectable.create()
调用
create
函数时发生的情况,该函数使用
Selectable
来调用
This
-这是一个方法调用@伯吉:是的,你说得对。对不起,我的错误。如果调用函数时没有
new
运算符(或
apply
call
方法),则对象
this
将是全局窗口对象。这就是调用
Selectable.create()
@jcbp:No.
Selectable.create()
调用
create
函数时发生的情况,该函数使用
Selectable
来调用
This
-这是一个方法调用@伯吉:是的,你说得对。对不起,我的错误。如果您使用ES6,您将执行
this.answers=Object.assign({},values)
如果您使用ES6,您将执行
this.answers=Object.assign({},values)