Here's My Code:
<script>
ABC = function () {
THIS.A;
this.b;
}
Abc.prototype = {
Getdata:function () {
var c = function (num) {
alert (num);
this.b = num;
}
C (' 12345 ');
},
Cleardata:function () {
This.getdata ();
alert (this.b);
}
}
var d = new ABC ();
D.cleardata ();
</script>
In this section:
var c = function (num) {
alert (num);
this.b = num;
}
C (' 12345 ');
I'd like to pass the NUM to the this.b that I started to define; But this is not possible, I do not know how to write it? Now the format can not be changed, the only change is C = function () {The Side of things}
You can understand this: reference:
function functionname (ARG) {...} ;
functionname (argvalue); the use of functions in this form is most familiar to everyone.
"()" (parentheses) can turn a parcel into a "noun" in the statement. Reference:
(function (formal parameter) {functions}) enclose an anonymous function in parentheses so that it is equivalent to a noun for other parts of the code.
So, quote:
(function (formal parameter) {functions body}) (argument) and the most common reference above:
The use of function names (arguments) is as easy to understand as the meaning of defining an anonymous function and calling it immediately. Reference:
The C = (function (which) {return function (num) {alert (num); which.b = num}}) (this) defines an anonymous function and calls immediately, which returns an anonymous function that is assigned the function named C.
Here the This object is passed as an argument to the formal parameter which, and the reference to the ABC instance object is provided to the internal anonymous function.
So C becomes a function to access the ABC instance object.
Technical article, I like
You can also write this
<script> ABC = function () {THIS.A; this.b; } Abc.prototype = {getdata:function () {//var o = this; THIS.C = function (num) {alert (num); this.b = num; } this.c (' 12345 '); }, Cleardata:function () {this.getdata (); alert (this.b); } var d = new ABC (); D.cleardata (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Using the Bind method
<script> Function.prototype.bind = Function (obj) {var temp = this; return function () {temp.apply (obj,arguments); } ABC = function () {THIS.A; this.b; } Abc.prototype = {getdata:function () {var c = (function (num) {alert (num); this.b = num; }). bind (this); C (' 12345 '); }, Cleardata:function () {this.getdata (); alert (this.b); } var d = new ABC (); D.cleardata (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]