Object methods assigned to variables or function arguments fail when invoked

后端 未结 5 943
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 13:11

I\'m learning javascript right now, seems like beautiful functional language to me, it is wonderful move from PHP, I should have done this earlier. Although, I cannot figure

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-21 13:47

    Your reference to the method has lost its knowledge of what it was a method of. This isn't so much good practice as just the way JS works.

    You can do:

    var v1 = /[abc]/;
    v1.test('a');    
    

    If you must encapsulate the test method, then you could do:

    var v1 = function(str){
        return /[abc]/.test(str);
    };
    v1('a');
    

提交回复
热议问题