简体   繁体   English

为什么在javascript应用中给出与直接调用不同的结果?

[英]Why in javascript apply gives different result than direct call?

I have the following code 我有以下代码

var d = new Date();
Object.prototype.toString(d); //outputs  "[object Object]"
Object.prototype.toString.apply(d); //outputs "[object Date]"

Why is this difference and what's going on? 为什么会出现这种差异以及发生了什么?

edit: 编辑:

d.toString() // outputs "Tue Nov 06 2012 ..."

So from where does the Date in "[object Date]" comes from. 那么“[对象日期]”中的日期来自何处。 Is it the native code of the browser that do the trick? 它是浏览器的本机代码吗?

Object.prototype.toString(d);

converts Object.prototype to string and ignores its argument. Object.prototype转换为string并忽略其参数。 In

Object.prototype.ToString.apply(d);

d gets passed as this to the ToString method (as if d.toString() with toString referring to Object.prototype.toString was called), which is what the method respects. d被作为传递thisToString方法(好像d.toString()toStringObject.prototype.toString被称为),这是什么方法方面。

See Function#apply and Object#toString 请参见Function#applyObject#toString

The parameter is ignored in the first call. 在第一次调用中忽略该参数。 You are calling the toString method on the Object.prototype object, basically the same as: 您正在调用Object.prototype对象上的toString方法,基本上与:

{}.toString(); //outputs  "[object Object]"

In the second call you are calling the toString method for Object but applying the Date object as its context. 在第二个调用中,您正在为Object调用toString方法,但将Date对象应用为其上下文。 The method returns the type of the object as a string, compared the toString method of the Date object which would instead return the value of the Date object as a string. 该方法返回该对象为字符串的类型,相比较toString所述的方法Date对象这将代替返回的值Date对象为字符串。

Another explanation is that Object.prototype.toString operates on its this object. 另一种解释是, Object.prototype.toString其上运行this对象。 A function's this is set by how you call it so when you do: 函数的this由你怎么称呼它,当你设置:

Object.prototype.toString();

the toString function's this is the Object.prototype object. toString函数thisObject.prototype对象。 When you call it as: 当你把它称为:

Object.prototype.toString.apply(d);

its this is the object referenced by d (a Date object). this是由所引用的对象d (Date对象)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么.call和.apply比JavaScript中的直接函数调用慢? - Why are .call and .apply slower than a direct function call in JavaScript? 包括样式表 <head> 给出不同于javascript附加到头部的结果 - Including stylesheet in <head> gives different result than javascript append to head 为什么将 blob 转换为数据 URI 会导致与直接数据 URI 方法不同的 URI? - Why does converting a blob to a data URI result in a different URI than the direct data URI method? 为什么过滤可观察订阅的结果会得到与过滤可观察本身不同的结果 - Why filtering the result of an observable subscription gives a different result than filtering observable itself 为什么 .NET 中的 AEC 加密产生与 JavaScript 不同的结果? - Why AEC encryption in .NET yields different result than JavaScript? 减去 int 给出与加法不同的结果 - subtracting int gives different result than addition 为什么结果不一样?? JavaScript - why the result is different?? JavaScript 一键调用两个javascript函数所得到的结果与两次单击所得到的结果不同 - Calling two javascript functions with one button click gives a different result than with two JavaScript - 使用require读取JSON文件会产生与fs.readFileSync不同的结果 - JavaScript - reading JSON file with require gives different result than fs.readFileSync Date 的不同给出 null 结果为 javascript - Different of Date gives a null result in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM