文章目录
1、利用构造函数创建对象
- 构造函数是一种特殊的函数,主要用来初始化对象,即为对象成员变量附初始值,它总与
new
一起使用。我们可以把对象中一些公共属性和方法抽取出来,然后封装到这个函数里面。 - new 在执行时会做的四件事情:
- 在内存中创建一个新的空对象。
- 让 this 指向这个新的对象。
- 执行构造函数里面的代码,给这个新对象添加属性和方法。
- 返回这个新对象(所以构造函数里面不需要 return)
// 利用构造函数创建对象
function Star(uname, age){
this.uname = uname
this.age = age
this.sing = function() {
console.log('I can sing')
}
}
let a = new Star('毛不易', 20)
console.log(a) //Star {uname: '毛不易', age: 20, sing: ƒ}
a.sing() //I can sing
2、实例成员和静态成员
- 实例成员:构造函数内部通过
this
添加的成员。只能通过实例化的对象来访问。 - 静态成员:在构造函数本身上添加的成员。只能通过构造函数来访问。
// 构造函数中的属性和方法我们称为成员 成员可以添加
function Star(uname, age){
this.uname = uname
this.age = age
this.sing = function() {
console.log('I can sing');
}
}
let a = new Star('毛不易', 20)
// 1. 实例成员就是构造函数内部通过this添加的成员 uname age sing 就是实例成员
// 实例成员只能通过实例化的对象来访问
console.log(a.uname) //毛不易
a.sing() //I can sing
// console.log(Star.uname) //undefined 不可以通过构造函数来访问实例成员
// 2.静态成员 在构造函数本身上添加的成员 sex 就是静态成员
Star.sex = '男'
// 静态成员只能通过构造函数来访问
console.log(Star.sex) //男
// console.log(a.sex) //undefined 不能通过对象来访问
3、构造函数原型对象 prototype
- 构造函数的问题:存在浪费内存的问题。
function Star(uname, age){
this.uname = uname
this.age = age
this.sing = function() {
console.log('I can sing');
}
}
let a = new Star('毛不易', 20)
let b = new Star('水木年华', 25)
console.log(a.sing === b.sing) //false 因为会创建两个函数
- 构造函数原型 prototype:构造函数通过原型分配的函数是所有对象所
共享的
。
- JavaScript 规定,每一个构造函数都有一个 prototype 属性,指向另一个对象。注意这个 prototype 就是一个对象,这个对象的所有属性和方法,都会被构造函数所拥有。
- 我们可以把那些不变的方法,直接定义在 prototype 对象上,这样所有的实例就可以共享这些方法。
- 原型是什么?一个对象,我们也成为 prototype 为 原型对象。
- 原型的作用:
共享方法
。
function Star(uname, age){
this.uname = uname
this.age = age
}
Star.prototype.sing = function(){
console.log('I can sing')
}
let a = new Star('毛不易', 20)
let b = new Star('水木年华', 25)
console.log(a.sing === b.sing)
a.sing() //I can sing
b.sing() //I can sing
4、对象原型 __proto__
- 对象都会有一个属性
__proto__
指向构造函数的 prototype 原型对象,之所以我们对象可以使用构造函数 prototype 原型对象的属性和方法,就是因为对象有__proto__
原型的存在。 __proto__
对象原型和原型对象 prototype 是等价的__proto__
对象原型的意义在于为对象的查找机制提供一个方向,或者说一条线路,但是它是一个非标准属性,因此在实际开发中,不可以使用和这个属性,它只是内部指向原型对象 prototype。
function Star(uname, age){
this.uname = uname
this.age = age
}
Star.prototype.sing = function(){
console.log('I can sing');
}
let a = new Star('毛不易', 20)
let b = new Star('水木年华', 25)
a.sing() //I can sing
console.log(a) //Star {uname: '毛不易', age: 20} 对象身上系统自己添加一个 __proto__ 指向我们构造函数的原型对象
console.log(a.__proto__ === Star.prototype) //true
// 方法查找规则:
// 如果a 对象上有sing 方法 就执行这个对象上的 sing
// 如果没有sing 这个方法 因为有__proto__的存在 就去构造函数原型对象 prototype 身上去查找sing 这个方法
5、constructor 构造函数
- 对象原型(
__proto__
)和构造函数(prototype
)原型对象里面都有一个constructor
属性,constructor
我们称为构造函数,因为它指回构造函数本身。 constructor
主要用于记录该对象引用于哪个构造函数,它可以让原型对象重新指向原来的构造函数。- 如果我们修改了原来的原型对象,给原型对象赋值的是一个对象,则需要手动的利用
constructor
这个属性指回原来的构造函数。
function Star(uname, age){
this.uname = uname
this.age = age
}
// 如果我们修改了原来的原型对象 给原型对象赋值的是一个对象 则需要手动的利用 constructor 这个属性指回 原来的构造函数
Star.prototype = {
constructor: Star,
sing(){
console.log('I can sing')
},
movie(){
console.log('I can perform in a film')
}
}
let a = new Star('毛不易', 20)
let b = new Star('水木年华', 25)
console.log(Star.prototype)
console.log(a.__proto__)
console.log(Star.prototype === a.__proto__)
console.log(Star.prototype.constructor)
console.log(a.__proto__.constructor)
如果不在 Star 原型对象上添加:
constructor: Star
,就不会有构造函数。
- 构造函数、实例、原型对象三者之间的关系
6、原型链
- 只要有对象就有
__proto__
原型。
function Star(uname, age){
this.uname = uname
this.age = age
}
Star.prototype.sing = function(){
console.log('I can sing')
}
let a = new Star('毛不易', 20)
console.log(a.__proto__ === Star.prototype) //true
// 1.只要有对象就有 __proto__ 原型 指向原型对象
console.log(a.__proto__) //指向Star.prototype
// 2.我们Star原型对象里面的__proto__原型指向的是 Object.prototype
console.log(Star.prototype.__proto__) //指向Object.prototype
// 3.我们Object.prototype 原型对象里面的__proto__原型 指向为 null
console.log(Object.prototype.__proto__) //指向 null
7、JavaScript 成员的查找机制(规则)
- 一层一层的查找,如果多层都含有某个属性,会就近选择。
第一层
function Star(uname, age){
this.uname = uname
this.age = age
}
Star.prototype.sing = function(){
console.log('I can sing')
}
// Star.prototype.sex = '女'
// Object.prototype.sex = '不详'
let a = new Star('张三', 20)
a.sex = '男'
console.log(a.sex) //男
第二层
function Star(uname, age){
this.uname = uname
this.age = age
}
Star.prototype.sing = function(){
console.log('I can sing')
}
Star.prototype.sex = '女'
// Object.prototype.sex = '不详'
let a = new Star('张三', 20)
// a.sex = '男'
console.log(a.sex) //女
第三层
function Star(uname, age){
this.uname = uname
this.age = age
}
Star.prototype.sing = function(){
console.log('I can sing')
}
// Star.prototype.sex = '女'
Object.prototype.sex = '不详'
let a = new Star('张三', 20)
// a.sex = '男'
console.log(a.sex) //不详
多层用最近的一层
function Star(uname, age){
this.uname = uname
this.age = age
}
Star.prototype.sing = function(){
console.log('I can sing')
}
Star.prototype.sex = '女'
Object.prototype.sex = '不详'
let a = new Star('张三', 20)
a.sex = '男'
console.log(a.sex) //男
8、原型对象中的 this 指向
- 构造函数里面 this 指向的是对象实例。
- 原型对象函数里面的 this 指向的是实例对象。
function Star(uname, age){
this.uname = uname
this.age = age
}
let that
Star.prototype.sing = function(){
console.log('I can sing')
that = this
}
let a = new Star('张三', 20)
// 1.构造函数里面 this 指向的是对象实例a
a.sing() //I can sing
// 2.原型对象函数里面的this 指向的是实例对象a
console.log(that === a) //true
9、扩展内置对象
- 可以通过原型对象,对原来的内置对象进行扩展自定义的方法。
// 原型对象的应用 扩展内置对象方法
console.log(Array.prototype)
Array.prototype.sum = function() {
let sum = 0
for(let i = 0; i < this.length; i++){
sum += this[i]
}
return sum
}
let arr = [1, 2, 3]
console.log(arr.sum())
console.log(Array.prototype)
let arr1 = new Array(2, 3, 4)
console.log(arr1.sum())
- 数组和字符串内置对象不能给原型对象覆盖操作
Array.prototype = {}
,只能是Array.prototype.xxx = function() {}
的方式。
Array.prototype = {
sum(){
let sum = 0
for(let i = 0; i < this.length; i++){
sum += this[i]
}
return sum
}
}
let arr = [1, 2, 3]
console.log(arr.sum())
console.log(Array.prototype)
let arr1 = new Array(2, 3, 4)
console.log(arr1.sum())