Home > Web Front-end > JS Tutorial > body text

js object external access or call problem_javascript skills

WBOY
Release: 2016-05-16 18:58:20
Original
1188 people have browsed it

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


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Use bind method

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute <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>]<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>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template