The usage of the Javascript method call and apply(what is the new and object.create difference.)(What does call(null) mean?)
2013-04-13 16:34
746 查看
firstly please review a code sample below .
the output is "joe" instead of the "wq".
so, found the mystery the call ?
Another case need to be mentioned here , the firstArg of call could be null.
As the MDN explained.
thisArg
The value of this provided for the call to fun. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.
Simply put it by a little code.
In the strict mode ,this is null. But in the not strict mode . this is window or global object.
see also:
http://stackoverflow.com/questions/4166616/understanding-the-difference-between-object-create-and-new-somefunction-in-j
<html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript"> /**/ $(function(){ var baseObj=function(){ this.firstname="joe"; this.lastname="wang"; }; var childObj=function(){ this.firstname="wq"; }; var b= new baseObj(); var c = new childObj(); baseObj.call(c); alert(c.firstname); }); </script> </head> <body> </body> </html>
the output is "joe" instead of the "wq".
so, found the mystery the call ?
Another case need to be mentioned here , the firstArg of call could be null.
As the MDN explained.
thisArg
The value of this provided for the call to fun. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.
Simply put it by a little code.
<script>'use strict'function fun(){ alert(this);} fun.call(null);</script>
In the strict mode ,this is null. But in the not strict mode . this is window or global object.
see also:
http://stackoverflow.com/questions/4166616/understanding-the-difference-between-object-create-and-new-somefunction-in-j
相关文章推荐
- What is the difference between call and apply?(关键词:call/apply/invoke)
- what is the difference between json and javascript object.
- what is the difference of select single and select up to one row in abap
- What is the difference between objectForKey and valueForKey?
- What is the difference between SVD and matrix factorization in context of recommendation engine?
- What is the difference between Array.Clone and Array.Copy Method
- What is the difference between << and += while injecting elements to Hash.new([])?
- what is the difference between "isempty"and "isnull"in the qt bool QString::isEmpty () const Retur.
- Javascript Object Method Properties console.log View all methods and properties of the object
- What is the difference between new/delete and malloc/free?
- What is the difference between new and malloc()?
- C++ Memory Management : What is the difference between malloc/free and new/delete?
- OpenStack: What is the difference between Block storage and Object storage?Also, In what way they ar
- why we use _MainText_ST and what does the mean of TRANSFORM_TEX
- What is the difference between @staticmethod and @classmethod in Python?
- JavaScript closure: difference between call and apply as well as use them to create inheritance
- About the difference of href='javascript:void(0)' and href=‘#’
- What is junction object and how to create it?
- What is the difference between application server and web server?
- What is the difference between getWidth/heigth() and getMeasuredWidth/Heigth() in Android SDK?