The problem that caused the owner to blur is actually the question that this points to. You can use alert to figure out this and see what they point to separately. I believe you will understand it! This in the c you wrote actually points to c rather than abc! The following is my code:
Script
Abc = function (){
This.;
This. B;
}
Abc. prototype = {
GetData: function (){
Var c = function (num ){
Alert (num );
This. B = num;
}
C ('20140901 ');
},
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 ('20140901 ');
I want to pass the defined num to this. B. But this is not acceptable. I don't know how to write it? The current format cannot be changed. Only c = function () {the content here} can be changed}
It can be understood as follows: Reference:
Function functionName (arg ){......};
FunctionName (argvalue); functions in this form are most familiar to everyone.
"()" (Parentheses) can change the statement enclosed in it into a "noun ". Reference:
(Function (form parameter) {function body}) wraps an anonymous function in brackets so that it is equivalent to a "noun" for other parts of the Code ".
Therefore, reference:
(Function (form parameter) {function body}) (real parameter) and the above most common reference:
Function names (real parameters) are easy to understand. They define an anonymous function and call it immediately. Reference:
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 and is assigned the name c.
Here, this object is passed as a real parameter to the which parameter, and the reference of the abc instance object is provided to the internal anonymous function.
C becomes a function that can access abc instance objects.
Technical Text
You can also write it like 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 ('20140901');}, clearData: function () {this. getData (); alert (this. b) ;}} var d = new abc (); d. clearData (); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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 ('20140901') ;}, clearData: function () {this. getData (); alert (this. b) ;}} var d = new abc (); d. clearData (); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]