Js object external access or call question _ javascript skills

Source: Internet
Author: User
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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.