使用[] .push.call()修改对象的长度 [英] Using [].push.call() to modify an object's length

查看:85
本文介绍了使用[] .push.call()修改对象的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法复制 MDN的示例(«以类似数组的方式使用对象»)。

Cannot reproduce MDN's example («Using an object in an array-like fashion»).

  let obj = {
    length: 0,
    addEl: function (element) {
       [].push.call(this, element);
    };
  };
  // Node REPL still expect me to do something, so there's an error. Why?

你们能解释一下这里出什么问题了吗?似乎我对这里的机制也不了解:

Could you, guys, explain what's wrong here? Also it seems that I don't get the point with the mechanics here:

// from the example:
obj.addElem({});
obj.addElem({});
console.log(obj.length);
// → 2

如果我们用不同的参数调用函数,而不是 {} ,可以吗?如果没有,那为什么我们应该确切地使用 {} 上下文是什么: addEl 方法或对象本身?如果是第二个函数,为什么不使用 addEl 函数:它不是数组函数,因此它应该有自己的 this (和,我想我会使用类似 objThis = this; 属性的东西。)

What if we call the function with some different agrument, not {}, will it work? And if it won't, then why we should use {} exactly? What is the this context here: addEl method or the object itself? If the second, why not addEl function: it's not an array function, so it should have its own this (and, I guess, I'd use something like objThis = this; property).

另一个相关的问题是此处。 p>

One more related question is here.

推荐答案

帖子中的代码有错别字:

The code in your post has some typos:

  let obj = {
    length: 0,
    addEl: function (element) {
       [].push.call(this, element);
    };
     ^ syntax error
  };
  // Node REPL still expect me to do something, so there's an error. Why?

正如您在代码注释中所怀疑的那样,
存在语法错误,该错误我为你标记。
删除该分号。

As you suspected in your comment in the code, there is a syntax error, which I marked for you. Remove that semicolon.

然后,在尝试示例时,您写了 obj.addElem , b $ b,但在上述对象文字中,您有 addEl

And then, when trying the example you wrote obj.addElem, but in the above object literal you have addEl.

如果您的示例可以正常工作只需复制粘贴即可。

The example should work just fine, if you simply copy-paste it.


var obj = {
    length: 0,

    addElem: function addElem(elem) {
        // obj.length is automatically incremented 
        // every time an element is added.
        [].push.call(this, elem);
    }
};

// Let's add some empty objects just to illustrate.
obj.addElem({});
obj.addElem({});
console.log(obj.length);
// → 2









如果我们用不同的参数而不是 {} 来调用函数,它将起作用吗?

What if we call the function with some different argument, not {}, will it work?

可以。为什么不呢? JavaScript中的数组可以包含不同类型的值。
不需要是同质的,
,所以是的,您可以插入除 {} 之外的其他内容。

Sure it will. Why wouldn't it? An array in JavaScript can contain values of different types. It doesn't need to be homogeneous, so yes, you can insert other things than {}.


this 的上下文是什么: addEl 方法或对象本身?

What is the this context here: addEl method or the object itself?

在其上调用方法的对象。因此它是 obj
这就是方法调用的工作方式。
调用 obj.something()时, something <中的 this 将是 obj

It's the object on which the method is called. So it's obj. This is how method invocation works. When you call obj.something(), the this inside something will be the obj.

如果您对此示例仍有疑问,请随时发表评论。

If you still have some doubts about this example, feel free to drop a comment.

这篇关于使用[] .push.call()修改对象的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆