zl程序教程

您现在的位置是:首页 >  Python

当前栏目

javascript new调用构造函数

2023-03-15 23:18:56 时间

javascript

1、内存中创建新的对象。

2、新对象的prototype特性被赋值为构造函数的属性。

3、this指向构造函数新对象。

4、执行构造函数的代码。

为新对象添加属性。

5、构造函数没有返回对象,则返回新对象。

实例

/除了声明函数也可以写成表达式
//Let Person=function (name,age){
  function Person(name,age){
    //let o=new Object();
    this.name=name;
    this.age=age;
    this.sayName=function(){
        console.log(this.name);
    };
    //return o;
}
Person.prototype=[];
let p1=new Person("Greg",27);
console.log(Person.prototype);
console.log(p1.prototype);
console.log(p1);

以上就是JavaScript new调用构造函数的方法,希望对大家有所帮助。更多Javascript学习指路:Javascript