javascript当中call()和apply()用法

13)call()和apply()
马克-to-win:call是在特定的作用域中调用函数。
例 3.13.1
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
    </title>
    <script type="text/javascript">
        function A(name) {
            this.name = name;
        }
         A.prototype.info = function()
         {
         /*如果下局解开屏蔽,最后结果则打印出就为A,结论就是在call时,先在A里找this.name,如果找不着,则用b环境里找,现在A的构造函数从来没有执行过,所以最后用的是B环境的name,见下一个例子*/
                //this.name="A";
                return "A的名字是 "+this.name;
         }
        function B(agev)  {
            this.age=agev;
            this.name="I am B"
        }
            var b = new B(2);
    var tmp =A.prototype.info;
    document.writeln(tmp.call(b));

    var zhanwei="zhanwei";

    </script>
</head>
<body>

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44594249/article/details/100285106

posted @ 2021-11-10 12:13  malala  阅读(15)  评论(0)    收藏  举报