bitsmix
V2EX  ›  问与答

一个 javascript 的小问题

  •  
  •   bitsmix · Aug 19, 2012 · 4056 views
    This topic created in 5195 days ago, the information mentioned may be changed or developed.
    简单来说就是我创建了一个 String.prototype 的方法。只有 return this

    结果从 string 变成了 object 这是为啥呢?

    更详细的内容我贴在 StackOverflow 了。

    那边看起来更方便

    http://stackoverflow.com/questions/12020463/javascript-string-object-v-s-just-string


    Thanks!
    2 replies  •  1970-01-01 08:00:00 +08:00
    dreampuf
    dreampuf
    Aug 19, 2012❤️1
    1
    转一个,stackoverflow已经有人回答你了。
    http://stackoverflow.com/questions/5146591/javascript-wtf-a-string-prototypes-this-doesnt-return-a-string
    The following steps are performed when control enters the execution context for function code contained in function object F, a caller provided thisValue, and a caller provided argumentsList:

    If the function code is strict code, set the ThisBinding to thisValue.
    Else if thisValue is null or undefined, set the ThisBinding to the global object.
    Else if Type(thisValue) is not Object, set the ThisBinding to ToObject(thisValue).
    Else set the ThisBinding to thisValue
    bitsmix
    bitsmix
    OP
    Aug 19, 2012
    2
    @dreampuf thx, 我后来也发现这个是一个重复的问题了。之前脑子卡壳了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1984 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 15:15 · PVG 23:15 · LAX 07:15 · JFK 10:15
    ♥ Do have faith in what you're doing.