Javascript redefine and override existing function body

后端 未结 8 2072
难免孤独
难免孤独 2020-12-24 03:12

I am wondering can we still change the function body once it is constructed ?

     var O = function(someValue){
           this.hello = function(){
                 


        
8条回答
  •  一个人的身影
    2020-12-24 04:09

    If I recall correctly, functions that are direct members of objects take precedence over like-named members of that object's prototype. Therefore, O.prototype.hello is usurped by O.hello, even though the former is defined later in the code.

    The reason someValue isn't available to your O.prototype.hello is because the scope of someValue is constrained to the constructor function and any functions defined or executed within it. Since O.prototype.hello is defined outside the scope of the O constructor, it doesn't know about someValue

提交回复
热议问题