function Test (word) {
    console.log (word);
}

Test('哈哈,我是函数');

new Test('哈哈,我是对象');


//将以上的调用方式换种通俗易懂的方式

Test.call("哈哈,我是函数");     //相当于Test();

//相当于new Test();
var obj = {};
obj._proto_ = Test.prototype;
Test.call(obj);

 

本质的区别就是,两次调用之中的this不同。调用Test('...');的时候,里面的this是顶级对象window,返回值是undefined。调用new Test('...');的时候,它会先new一个对象,置类型为Test,之后把它作为this执行Test函数,最后再把对象返回。

 

 

 

来源于sf

相关文章:

  • 2021-08-13
  • 2021-12-28
  • 2022-02-04
  • 2022-12-23
  • 2021-10-11
  • 2021-12-22
  • 2021-12-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
相关资源
相似解决方案