Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new 运算符的执行过程 #29

Open
Yuanfang-fe opened this issue Jun 1, 2021 · 0 comments
Open

new 运算符的执行过程 #29

Yuanfang-fe opened this issue Jun 1, 2021 · 0 comments

Comments

@Yuanfang-fe
Copy link
Owner

Yuanfang-fe commented Jun 1, 2021

  1. 在内存中创建一个新对象。
  2. 新对象内部的[[prototype]]特性被赋值为构造函数的prototype属性
  3. 构造函数内部的this被赋值为这个新对象(即this指向新对象)
  4. 执行构造函数内部的代码(给新对象添加属性)
  5. 如果构造函数返回非空对象,则返回该对象;否则,返回刚创建的新对象(即this)
function Person(name, age, job) {
  this.name = name;
  this.age  = age;
  this.job = job;
  this.sayName = function() {
    console.log(this.name);
  }
}

let person1 = new Person('nick', 29, '程序员')
let person2 = new Person('tom', 28, '摄影师')

person1.sayName();    // nick
person2.sayName();    // tom

image

原型链关系:
构造函数原型链 (4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant