Any way to modify 'this' in JavaScript?

后端 未结 4 1409
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 08:56

I found this answer: How to modify "this" in javascript

But I did not like it. Surely there must be a way to somehow modify this?

4条回答
  •  既然无缘
    2021-01-06 09:24

    See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

    Simple types (except objects and arrays) is immutable. You can't modify it and save in source variable.

    function foo() {
        this.val+=10;
    }
    
    a=function(obj) {
        a=foo.bind(obj);
    }
    

    But you can wrap it into object. See: http://jsfiddle.net/5dhm8fsn/

提交回复
热议问题