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

javascript new执行过程 #56

Open
spring-fe opened this issue Jan 12, 2019 · 0 comments
Open

javascript new执行过程 #56

spring-fe opened this issue Jan 12, 2019 · 0 comments

Comments

@spring-fe
Copy link
Owner

spring-fe commented Jan 12, 2019

function Person(name, age) {
    this.name = name;
    this.age = age;
}
Person.prototype.say = function() {
    alert('hi, my name is' + this.name);
}

var person = new Person();
person.say();

new关键字执行过程:

  1. 创建一个新的对象。var object = new Object();
  2. Person.prototype的值赋值给o.proto。o.proto = Person.prototype;
  3. 执行Person构造函数。执行时,参数会被传入,同时o对象会赋值给this。
  4. 如果构造函数返回了一个对象,那么返回这个对象。如果构造函数没有返回对象,那么返回new出来的object对象。
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