admin管理员组文章数量:1289830
From what I understand, the former will:
- Find the
toString
method on Object - call it on
value
but withthis
bound tovalue
And value.toString()
will.
- Find the
toString
method somewhere invalue
's prototype chain - Call
toString
on value bound withthis
as value via the function invocation pattern
So the difference is if there is an overridden toString
method in value... it will use that.
My question is:
- Is that the only difference?
- Conversely, is this pattern the standard pattern to use if we want to be guaranteed we're calling
Parent
's method and not potentially some overridden byChild
? (In this case Parent = Object, Child = the class value es from, if we're thinking classically, and method = toString.)
From what I understand, the former will:
- Find the
toString
method on Object - call it on
value
but withthis
bound tovalue
And value.toString()
will.
- Find the
toString
method somewhere invalue
's prototype chain - Call
toString
on value bound withthis
as value via the function invocation pattern
So the difference is if there is an overridden toString
method in value... it will use that.
My question is:
- Is that the only difference?
- Conversely, is this pattern the standard pattern to use if we want to be guaranteed we're calling
Parent
's method and not potentially some overridden byChild
? (In this case Parent = Object, Child = the class value es from, if we're thinking classically, and method = toString.)
- I don't understand your last paragraph. What are you asking? – Explosion Pills Commented Mar 6, 2013 at 1:44
- @ExplosionPills thanks, edited to hopefully be more clear. – djechlin Commented Mar 6, 2013 at 1:46
3 Answers
Reset to default 6Object.prototype.toString.apple(value)
will let you call on null
, while you use null.toString()
, it will produce an error.
Object.prototype.toString.apply(null);
>"[object Null]"
null.toString();
>TypeError: Cannot call method 'toString' of null
Object.prototype.toString
can be a different method than value.toString()
depending upon what the latter is.
> Object.prototype.toString.apply("asdfasdf")
"[object String]"
> "asdfasdf".toString()
"asdfasdf"
> Object.prototype.toString.apply(new Date)
"[object Date]"
> (new Date).toString()
"Tue Mar 05 2013 20:45:57 GMT-0500 (Eastern Standard Time)"
.prototype[function].apply
(or .call
or .bind
) allow you to change the context of a method even though the context may not have such a method at all.
var o = {};
o.prototype = {x: function () { console.log('x'); }}
var y = {}
o.prototype.x.call(y)
y.x(); //error!
...so that is to say that
- It's not the only difference
- This doesn't necessarily have to do with a relationship between parent and child .. it just allows you to call one object's method (or any function) with a different object context.
Yes, you got this right. I don't usually see people calling Object.prototype.toString
directly like that though (it usually makes some sense to let objects override their toString
method) but its certainly very mon and remended for some other methods like Object.prototype.hasOwnProperty
.
本文标签: javascriptHow is ObjectprototypetoStringapply(value) different from valuetoString()Stack Overflow
版权声明:本文标题:javascript - How is Object.prototype.toString.apply(value) different from value.toString()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741486624a2381434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论