The following is my code:
<script> <br>abc = function(){ <br>this.a; <br>this.b; <br>} <br>abc.prototype = { <br>getData:function(){ <br>var c = function(num){ <br>alert(num); <br>this.b = num; <br>} <br>c('12345' ); <br>}, <br>clearData:function(){ <br>this.getData(); <br>alert(this.b); <br>} <br>} <br>var d = new abc(); <br>d.clearData(); <br></script>
In this part:
var c = function(num){
alert(num);
this.b = num;
}
c('12345');
I want to pass the obtained num to this.b that was defined at the beginning; but this is not possible. I don’t know how to write it. ? The current format cannot be changed, the only thing that can be changed is c = function(){ here}
can be understood like this: Quote:
function functionName(arg){...};
functionName(argvalue); Using functions in this form is the most familiar to everyone.
"()" (brackets) can turn the statement wrapped in it into a "noun". Quote:
(function(formal parameter){function body}) wraps an anonymous function in parentheses, making it equivalent to a "noun" to other parts of the code.
So, the reference:
(function(formal parameter){function body})(actual parameter) is as easy to understand as the most common reference above:
function name (actual parameter) Yes, it means defining an anonymous function and calling it immediately. Quote:
c = (function(which){return function(num){alert(num);which.b = num}})(this) defines an anonymous function and calls it immediately. This function returns an anonymous function , the returned function is assigned the name c.
Here, the this object is passed as an actual parameter to the formal parameter which, and the reference of the abc instance object is provided to the internal anonymous function.
So c becomes a function that can access the abc instance object.
Technical articles, I like
You can also write like this