zoukankan      html  css  js  c++  java
  • The usage of the Javascript method call and apply(what is the new and object.create difference.)(What does call(null) mean?)

    firstly please review a code sample below .

    <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

  • 相关阅读:
    PHP设计模式之工厂模式
    ThinkPHP删除栏目(多)
    斐波纳契数列递归和非递归算法
    单链表反转的实现
    找出n个数中最大的k个数
    实验四:掌握Linux系统的构建和调试方法
    npm如何上传自己的包
    简要谈一下部署时候的操作
    sass的基本语法及使用
    this 指向问题
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/3018622.html
Copyright © 2011-2022 走看看