正在加载文章
深入了解 JavaScript 中的对象。
深入了解 JavaScript 中的对象。
对象创建模式:
1. Object 构造函数模式
let p = new Object()
p.name = 'Tom'
p.age = 12
p.setName = function(name) {
this.name = name
}
2. 对象字面量模式
let p = {
name: 'tom',
age: 12,
setName: function(name) {
this.name = name
}
}
3. 工厂模式
function createPerson(name, age) {
let obj = {
name: name,
age: age,
setName: function(name) {
this.name = name
}
}
return obj
}
let p = createPerson('tom', 12)
4. 自定义构造函数模式
function Person(name, age) {
this.name = name
this.age = age
this.setName = function(name) {
this.name = name
}
}
let p = new Person('tom', 12)
let p2 = new Person('tom2', 12)
console.log(p, p2) // 两个对象都有setName 方法, 浪费内存空间
5. 构造函数 + 原型的组合模式
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.setName = function(name) {
this.name = name
}
let p1 = new Person('tom', 22)
let p2 = new Person('jack', 12)
console.log(p1, p2)
// 父类
function Parent() {
this.pName = 'parent'
}
Parent.prototype.showParent = function() {
console.log('This is Parent')
}
// 子类
function Sub() {
this.sName = 'sub'
}
// 要让子类继承父类的属性和方法,就需要使子类型的显式原型指向一个父类型的实例,原理是连接原型链
Sub.prototype = new Parent()
Sub.prototype.constructor = Sub
Sub.prototype.showSub = function() {
console.log('This is Sub')
}
let sub = new Sub()
console.log(sub.pName) // 'parent'
console.log(sub.sName) // 'sub'
sub.showParent() // 'This is Parent'
sub.showSub() // 'This is Sub'
function Person(name, age){
this.name = name
this.age = age
}
Person.prototype.setName = function(name){
this.name = name
}
function Student(name, age, salary){
Person.call(this, name, age) // 相当于this.Person(name, age)
this.salary = salary
}
Student.prototype = new Person() // 使Student继承Person
Student.prototype.constructor = Student // 使constructor指向对应的函数本身
Student.prototype.setSalary = function(salary){
this.salary = salary
}
var s = new Student("Tom",12,10000)
s.setName("Tommy") // 调用父类方法
s.setSalary(14000)
console.log(s.name,s.age,s.salary);
function Super() {
this.name = 'super_name'
this.age = 'super_age'
}
Super.prototype.sayName = function() {
console.log(this.name);
}
function Sub(gender) {
this.gender = 'sub_gender'
}
Sub.prototype = new Super() // 修改子类的原型对象,使之指向父类的实例
Sub.prototype.construtor = Sub
const sub = new Sub()
sub.sayName() // super_name
// 原型链方案存在的缺点:多个实例对引用类型的操作会被篡改。
function Super() {
this.name = 'super_name'
this.age = 'super_age'
}
Super.prototype.sayName = function() {
console.log(this.name);
}
function Sub() {
Super.call(this)
this.gender = 'sub_gender'
}
const sub = new Sub()
console.log(sub.name); // 只能继承父类在构造函数中的属性或方法,不能继承原型链中的属性或方法
console.log(sub.age);
console.log(sub.gender);
// 3. 组合继承(组合上述两种方法就是组合继承。用原型链实现对原型属性和方法的继承,用借用构造函数技术来实现实例属性的继承。)
function Super() {
this.name = 'super_name'
this.age = 'super_age'
}
Super.prototype.sayName = function() {
console.log(this.name);
}
function Sub() {
Super.apply(this) // 第二次调用Super():给sub实例写入两个属性name,age
this.gender = 'sub_gender'
}
Sub.prototype = new Super() // 第一次调用Super():给Sub.prototype写入两个属性name,age
Sub.prototype.construtor = Sub
const sub = new Sub()
console.log(sub.name); // 通过上述两种方式的组合,实现了构造函数和原型链中的继承
console.log(sub.age);
console.log(sub.gender);
sub.sayName()
// 缺点:实例对象sub上的两个属性屏蔽了其原型对象Sub.prototype的两个同名属性。所以,组合模式的缺点就是在创建子类对象的过程中调用了两次父类构造函数,使得子类的实例对象和原型对象中有两份相同的属性/方法。
// 4. 寄生组合继承
function Super() {
this.name = 'super_name'
this.age = 'super_age'
}
Super.prototype.sayName = function() {
console.log(this.name);
}
function Sub() {
Super.apply(this) // 第二步,新建子类实例时,调用父类构造函数,继承其属性/方法
this.gender = 'sub_gender'
}
(function() {
let prototype = Object.create(Super.prototype) // 第一步,根据父类的原型对象创建一个实例对象(和直接使用 new 创建实例对象相比,不会执行构造函数里面的代码,相当于创建的是一个空的实例对象,将实例对象的 __proto__指向 prototype)
Sub.prototype = prototype // 使子类原型对象指向这个k
prototype.constructor = Sub
})()
const sub = new Sub()
console.log(sub.name); // 通过上述两种方式的组合,实现了构造函数和原型链中的继承
console.log(sub.age);
console.log(sub.gender);
sub.sayName()
class Person {
constructor(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
// Getter
get name() {
return `${this.firstName}_${this.lastName}`
}
// Method
sayName() {
console.log(this.name);
}
}
class Student extends Person {
constructor(firstName, lastName, gender, age) {
super(firstName, lastName)
this.gender = gender
this.age = age
}
// Getter
get info() {
return {
name: this.name,
gender: this.gender,
age: this.age
}
}
// Method
sayInfo() {
console.log(this.info);
}
}
var stu = new Student('kobe', 'Bryant', 'male', 30)
stu.sayName() // 继承父类的方法
console.log(stu.name); // 继承父类的属性
console.log(stu.firstName);
console.log(stu.lastName);
63 Vuejs 响应式原理 深入理解 Vue 的响应式原理。 我们知道,在 Vue 里,当我们修改某个元素里的属性后,会触发页面层的更新,那些在页面中引用了该属性的部分也会发生相应的改变。 那么,Vue 是如何实现这种... 61 JS 函数进阶【3】 JavaScript 函数中的闭包问题。 闭包的定义 什么是闭包? 理解一:闭包是嵌套的内部函数(绝大多数人的理解) 理解二:闭包是包含被引用变量/函数的对象(少数人的理解) 可以使用 ch...