为什么在方法上调用(调用)和在obj上调用方法之间有区别 [英] why there is a difference between calling (call) on a method and calling the method on the obj
问题描述
对不起,我真的应该问为什么存在差异
sorry I really should of asked why there is a differnce ,
`Object.prototype.toString.call(o).slice(x,y);`
还有这个?
o.toString().slice(x.y);
//为什么它们不同,调用应更改被调用方法$ b的 this值$ b //和toString已被继承,
// why those are different , call should change the 'this' value for the called method // and toString is already inherited ,
var x = 44 ;
`Object.prototype.toString.call(x)`; //"[object Number]"
x.toString(); // '44'
推荐答案
您没有呼叫< a href = https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/call rel = nofollow> .call
上的方法:
You're not calling .call
on the method here:
Object.prototype.toString(o).slice(x,y);
相反,您只需调用方法(在原型对象上)以 o
作为参数。
Instead, you just call the method (on the prototype object) with o
as an argument.
要获取等效的方法来调用
To get an equivalent method to call to
o.toString().slice(x.y);
(在上调用方法o
对象,不带参数),则需要使用
(which calls the method on the o
object, with no arguments), you will need to use
o.toString.call(o).slice(x.y);
为什么
x.toString()
与Object.prototype.toString.call(x)
吗?
因为 x = 44
是一个数字,并且当您访问 x.toString时
,您将获得 Number.prototype.toString
方法而不是 Object.prototype.toString
方法。您也可以写
Because x = 44
is a number, and when you access x.toString
you get the Number.prototype.toString
method not the Object.prototype.toString
method. You could also write
Number.prototype.toString.call(x)
获得 44
。
这篇关于为什么在方法上调用(调用)和在obj上调用方法之间有区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!