Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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 余烬this.get Uncaught TypeError:Object#<;窗口>;没有方法';获取';_Javascript_Jquery_Ember.js - Fatal编程技术网

Javascript 余烬this.get Uncaught TypeError:Object#<;窗口>;没有方法';获取';

Javascript 余烬this.get Uncaught TypeError:Object#<;窗口>;没有方法';获取';,javascript,jquery,ember.js,Javascript,Jquery,Ember.js,我试图让这个代码在余烬工作,但它一直给我 “未捕获的TypeError:对象#没有方法'get'”在线 this.get('element_id')[item.id]=true为什么它不能访问元素\u id散列 function() { return Ember.ArrayProxy.extend({ box: null, content: [], element_id: {}, init: function() {

我试图让这个代码在余烬工作,但它一直给我 “未捕获的TypeError:对象#没有方法'get'”在线
this.get('element_id')[item.id]=true为什么它不能访问元素\u id散列

function() {
    return Ember.ArrayProxy.extend({
        box: null,
        content: [],
        element_id: {},

        init: function() {
            this._super();
            var items = this.get( 'box' ).getData();
            if ( items.get( 'length' ) ) {
                this.set( '[]', items );
            };

            // Initialize the hash
            items.forEach(function(item) {
                this.get('element_id')[item.id] = true;
            });             

        }
    });
}
))

使用该方法时,您可以传递一个目标对象,该对象将在上下文中设置为
this
,如文档中所述:

请注意,除了回调之外,还可以传递一个可选的目标对象,该对象将在上下文中设置为“this”。这是一种让迭代器函数访问当前对象的好方法

因此,您的代码现在应该是:

items.forEach(function(item) {
  this.get('element_id')[item.id] = true;
}, this); 

快速相关问题,当您使用getJSON时,这也不起作用,现在我在getson之外声明一个var backup=this;有类似的方法吗?好吧,不,你必须用闭包来解决这个问题。js中一个常见的命名约定是
var,它=this。你知道这对应用程序内存消耗非常有害吗?这是复制大块数据还是使用正确的指针?同样,我不是100%确定,但通常,它只是保留相同的对象,并通过“嵌套”函数访问它。所以我认为你可以安全地做到。