在这个Javascript语句中执行的.call()函数是什么? [英] What is the .call() function doing in this Javascript statement?

查看:67
本文介绍了在这个Javascript语句中执行的.call()函数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在积极学习javascript,我发现了以下声明:

I'm actively learning javascript, and I came across the following statement:

Object.prototype.toString.call([]); 

我不知道这意味着什么或它做了什么。

And I don't know what it means or what it does.

我对 .call 有一个模糊的理解,因为它允许你在不同对象的上下文中调用一个方法(我认为) ,但我很难理解 .call()函数在上述语句中扮演的角色。所以我想知道是否有人能解释 .call()在这里做什么?

I have a vague understanding of .call, in that it allows you to call a method in the context of a different object (I think), but I am having a hard time understanding what role the .call() function is playing in the above statement. So I was wondering if anyone could explain what .call() is doing here?

谢谢!!

推荐答案

调用方法设置 this 作为第一个参数传递的对象的调用函数的值,在您的示例中,您正在对Array对象执行 Object.prototype.toString 方法。

The call method sets the this value of the invoked function to the object passed as first argument, in your example, you are executing the Object.prototype.toString method on an Array object.

数组对象,有自己的 toString 方法( Array.prototype.toString )如果你调用 []。toString(); Object.prototype 中隐藏一个>将调用 Array.prototype 上的方法。

Array objects, have their own toString method (Array.prototype.toString) that shadows the one from Object.prototype, if you call [].toString(); the method on the Array.prototype will be invoked.

例如:

function test() {
  alert(this);
}
test.call("Hello"); // alerts "Hello"

另一个例子:

var alice = {
  firstName: 'Alice',
  lastName: 'Foo',
  getName: function () {
    return this.firstName + ' ' + this.lastName;
  }
};

var bob = {
  firstName: 'Bob',
  lastName: 'Bar',
};

alice.getName.call(bob); // "Bob Bar"

在上面的例子中,我们使用Alice的 Bob的对象上的getName 方法,这个值指向 bob ,所以方法就像它是在第二个对象上定义的那样工作。

In the above example, we use the Alice's getName method on the Bob's object, the this value points to bob, so the method works just as if it were defined on the second object.

现在让我们谈谈 Object.prototype.toString 方法。 JavaScript中的所有本机对象都包含一个名为 [[Class]]的内部属性 此属性包含一个字符串值,表示定义的规范分类对象, native 对象的可能值为:

Now let's talk about the Object.prototype.toString method. All native objects in JavaScript contain an internal property called [[Class]] this property contains a string value that represents the specification defined classification of an object, the possible values for native objects are:


  • 对象

  • 数组

  • 功能

  • 日期

  • RegExp

  • String

  • 数字

  • 布尔

  • 错误用于错误对象,例如 ReferenceError TypeError SyntaxError 错误

  • 全局数学对象的数学

  • JSON,用于ECMAScript 5th Ed上定义的全局JSON对象。规范。

  • 参数对象的参数(也在ES5规范。)

  • null(几天前在ES5勘误表

  • undefined

  • "Object"
  • "Array"
  • "Function"
  • "Date"
  • "RegExp"
  • "String"
  • "Number"
  • "Boolean"
  • "Error" for error objects such as instances of ReferenceError, TypeError, SyntaxError, Error, etc
  • "Math" for the global Math object
  • "JSON" for the global JSON object defined on the ECMAScript 5th Ed. spec.
  • "Arguments" for the arguments object (also introduced on the ES5 spec.)
  • "null" (introduced just a couple of days ago in the ES5 errata)
  • "undefined"

As我之前说过,属性是内部,没有办法改变它,规范没有提供任何操作符或内置函数来执行它,而你可以通过 Object.prototype.toString 方法访问它的方法。

As I've said before that property is internal, there is no way to change it, the specification doesn't provide any operator or built-in function to do it, and the only way you can access its value is through the Object.prototype.toString method.

这个方法返回一个字符串形成:

This method returns a string formed by:

"[object " + this.[[Class]] + "]"

仅用于说明目的,因为 [[Class]] 不能可以直接访问。

Only for expository purposes because [[Class]] cannot be accessed directly.

例如:

Object.prototype.toString.call([]);       // "[object Array]"
Object.prototype.toString.call(/foo/);    // "[object RegExp]"
Object.prototype.toString.call({});       // "[object Object]"
Object.prototype.toString.call(new Date); // "[object Date]"
// etc...

这是真的用于以安全的方式检测对象的,用于检测数组对象,它是使用最广泛的技术:

This is really useful to detect the kind of an object in a safe way, for detecting array objects, it's the most widely used technique:

function isArray(obj) {
  return Object.prototype.toString.call(obj) == '[object Array]';
}

可能很想使用 instanceof 运算符,但如果你在跨框架环境中工作,这种方式会导致问题,因为在一个框架上创建的数组对象不会是 instanceof 另一个的数组构造函数。

It might be tempting to use the instanceof operator, but that way will lead to problems if you work on cross-frame environments, because an array object created on one frame, will not be instanceof the Array constructor of another.

上述方法可以正常运行,因为该对象将包含其 [[Class]] 内部属性的值。

The above method will work without any problems, because the object will contain the value of its [[Class]] internal property intact.

参见:

  • instanceof considered harmful (or how to write a robust isArray)
  • Object.prototype.toString
  • Object Internal Properties and Methods

这篇关于在这个Javascript语句中执行的.call()函数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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