美文网首页
new关键字

new关键字

作者: zhulichao | 来源:发表于2020-07-22 09:11 被阅读0次
function FuncA(name) {
    this.name = name;
};
var a = new FuncA('this is functon A');
console.log(a.name); // 输出 this is functon A

new操作在背后做了如下操作。

// 创建一个object,假设叫x
var x = {}; 
// 把A.prototype赋给x.__proto__
x.__proto__ = FuncA.prototype;
// 调用A.call(x)
var result = FuncA.call(x);
// 如果result是对象则返回,否则返回空对象x
if (typeof(result) === "object"){
  return result;
}
return x;

因此也有:

function FuncB() {
  return {
    name: 'name'
  };
}
var b = new FuncB();
console.log(b.name); // 输出 name
function FuncC() {
  return 'name';
}
var c = new FuncC();
console.log(c); // 输出 空对象

相关文章

  • C++中new、operator new和placement n

    1. new (1)C++对象实例化的时候使用new关键字和不使用new关键字的区别 使用new是动态分配内存,这...

  • Java中生成实列

    1.new 一般我们使用Java关键字new生成实例。 Something obj=new Something()...

  • JavaScript new 关键字

    new 关键字 在JavaScript中, new 关键字用来创建一个类(模拟类)的实例对象。 实例化对象之后, ...

  • Java中创建对象的四种方式

    使用new关键字创建对象 使用new关键字创建对象是最常见的一种方式,但是使用new创建对象会增加耦合度。在开发中...

  • js学习札记-new关键字

    js 的new关键字解析的过程中引擎执行了很多步骤,我们可以自己写一个仿new的函数来实现new关键字。 说js ...

  • java中的构造方法

    构造器 构造方法 Constructor 构造器的作用:1、创建对象,必须和new关键字一起使用。new关键字的作...

  • Java四种创建对象方式

    1,创建的new关键字创建对象 new HelloWorld(); 2,使用newInstance()方法 例如:...

  • js基础-new关键字

    js 的new关键字解析的过程中引擎执行了很多步骤,我们可以自己写一个仿new的函数来实现new关键字。 js n...

  • Android Studio 看不到 DexClassLoade

    关键字: DexClassLoader throw new RuntimeException("Stub!")An...

  • Java-线程状态、ObjectMonitor

    关键字:线程状态、ObjectMonitor 状态分类 (1)新建状态(NEW),执行new Thread()后的...

网友评论

      本文标题:new关键字

      本文链接:https://www.haomeiwen.com/subject/faexhktx.html