创建对象的多种方式
https://blog.youkuaiyun.com/tinyint_813/article/details/115396219?spm=1001.2014.3001.5501
创建构造函数发生的事情
1. 基础知识
注意:构造函数名称首字母最好大写,在创建实例的时候用new关键字创建。
通过构造函数Student可以创建实例对象stu,stu的__proto__指向Student的原型对象,而Student的原型对象prototype的构造对象指向它本身。
function Student(name, age){
this.name = name;
this.age = age;
}
let stu = new Student("ccc","23");
console.log(stu);
console.log(stu.__proto__);
console.log(Student.prototype);
console.log(stu.__proto__ === Student.prototype);
console.log(stu.__proto__.constructor);
console.log(stu.__proto__.constructor === Student);
console.log(Student.prototype.constructor === Student);
得到的结果为
Student { name: 'ccc', age: '23' }
{}
{}
true
[Function: Student]
true
true
解释:任何构造函数都有原型对象prototype,默认为空对象。所以在不给它设值的时候,它默认为{}
2. 更改值的问题
①更改属性值(不为对象)
3.拓展
本文详细介绍了如何通过构造函数创建对象,包括实例化过程、原型对象的作用,以及如何更改属性和拓展原型。重点讲解了构造函数与原型的关系以及相关操作的实际效果。
958

被折叠的 条评论
为什么被折叠?



