New
1. 创建一个新对象
2. 将构造函数的作用域赋给新对象 (this指向这个新对象)
3. 执行构造函数中的代码,为这个新对象添加属性
4. 返回新对象
// 1.其本质就是一个普通的函数,和普通函数没有区别
// 2.构造函数在习惯上首字母大写,以用来区分普通函数
// 3.构造函数通过new来调用
// 使用同一个构造函数创建的对象,我们称为一类对象,也将一个构造函数称为是一个类。通过一个构造函数创建的对象,称为是一个该类的实例 instance。
// 在构造函数中,可以使用this来引用新的对象
function Person(){
console.log(this);
}
console.log(Person());
console.log(new Person());
function Person(name,age){
this.name = name;
this.age = age;
}
例子:
function Grade(chinese,math,english){
this.chinese = chinese;
this.math = math;
this.english = english;
this.avr = function(){
var a = (this.chinese+this.english+this.math)/3
return a
}
}
var my = new Grade(80,70,90)
console.log(my.avr())//80
自己的临时笔记