跟着coderwhy学习
1.对象的方法补充
- hasOwnProperty
- 对象是否有某一个属于自己的属性(不是在原型上的属性)
var obj = {
name: 'mint',
age: 18
}
var info = Object.create(obj, {
address: {
value: '北京市',
enumerable: true
}
})
// hasOwnProperty方法判断
console.log(info.hasOwnProperty('address')) //true
console.log(info.hasOwnProperty('name')) // false
- in/for in 操作符
- 判断某个属性是否在某个对象或者对象的原型上
console.log('address' in info) // ture
console.log('name' in info) // ture
// for in
for (var key in info) {
console.log(key)
}
// address
//name
// age
- instanceof
-
用于检测构造函数的pototype,是否出现在某个实例对象的原型链上
-
- isPrototypeOf
- 用于检测某个对象,是否出现在某个实例对象的原型链上
var obj = {
name: 'mint'
}
var info = Object.create(obj)
// 用于检测某个对象, 是否出现在某个实例对象的原型链上
console.log(obj.isPrototypeOf(info)) // true
2.原型继承关系
var obj = {
name: 'mint'
}
console.log(obj.__proto__)
// 对象里面是有一个__proto__对象: 隐式原型对象
// Foo是一个函数,那么他会有一个显示原型: Foo。prototype
// Foo.prototype来自哪里?
// 答案: 创建了一个函数, Foo。prototype = { constructor: Foo }
// Foo是一个对象。那么他会哟普一个隐式原型对象: Foo。__proto__
// Foo。__proto__ 来自哪里?
// Function.prototype = { constructor: Function }
// var Fpp = new Function() {}
function Foo() {
}
console.log(Foo.prototype === Foo.__proto__) //false
console.log(Foo.prototype.constructor) //[Function: Foo]
console.log(Foo.__proto__.constructor) //[Function: Function]
console.log(Function.prototype === Function.__proto__);//true
3.认识class定义类
- 我们会发现,按照前面的构造函数形式创建 类,不仅仅和编写普通的函数过于相似,而且代码并不容易理解。
- 在ES6(ECMAScript2015)新的标准中使用了class关键字来直接定义类;
- 但是类本质上依然是前面所讲的构造函数、原型链的语法糖而已;
- 所以学好了前面的构造函数、原型链更有利于我们理解类的概念和继承关系;
- 那么,如何使用class来定义一个类呢?
- 可以使用两种方式来声明类:类声明和类表达式;
class Person {}
var Student = class {}
4.类和构造函数的异同
var p = new Person
console.log(Person) // [class Person]
console.log(Person.prototype) // {}
console.log(Person.prototype.constructor) // [class Person]
console.log(p.__proto__ === Perosn.prototype) // true
console.log(typeof Person) // function
5.类的构造函数
- 如果我们希望在创建对象的时候给类传递一些参数,这个时候应该如何做呢?
- 每个类都可以有一个自己的构造函数(方法),这个方法的名称是固定的constructor;
- 当我们通过new操作符,操作一个类的时候会调用这个类的构造函数constructor;
- 每个类只能有一个构造函数,如果包含多个构造函数,那么会抛出异常;
- 当我们通过new关键字操作类的时候,会调用这个constructor函数,并且执行如下操作:
- 1.在内存中创建一个新的对象(空对象);
- 2.这个对象内部的[[prototype]]属性会被赋值为该类的prototype属性;
- 3.构造函数内部的this,会指向创建出来的新对象;
- 4.执行构造函数的内部代码(函数体代码);
- 5.如果构造函数没有返回非空对象,则返回创建出来的新对象;
// 类的声明 class Person { // 类的构造方法 // 注意: 一个类只能有一个构造函数 mint = {} // 1.在内存中创建一个对象 // 2.将类的原型prototype赋值给创建出来的对象 mint。__proto__ //3.将对象赋值给函数的this:new绑定 this = mint // 4.执行函数体中的代码 // 5.自动返回创创建出来的对象 constructor(name, age) { this.name = name this.age = age } } var p1 = new Person('mint', 18) var p2 = new Person('pika', 20) console.log(p1); console.log(p2);
6.类的实例方法
- 在上面我们定义的属性都是直接放到了this上,也就意味着它是放到了创建出来的新对象中:
-
在前面我们说过对于实例的方法,我们是希望放到原型上的,这样可以被多个实例来共享;
-
这个时候我们可以直接在类中定义;
constructor(name, age, addres) { this.name = name this.age = age this.address = '武汉市' } eating() { console.log(this.name + ' eating~') } running() { console.log(this.name + ' running~') } -
7.类的访问器方法
- 讲对象的属性描述符时有讲过对象可以添加setter和getter函数的,那么类也是可以的:
class Person {
constructor(address) {
this._address = name
}
}
get address() {
console.log('调用get的address方法')
return this._address
}
set address(newAddress) {
console.log('调用set的address方法')
this._address = newAddress
}
8.类的静态方法
- 静态方法通常用于定义直接使用类来执行的方法,不需要有类的实例,使用static关键字来定义:
class Person {
constructor(age) {
this.age = age
}
static create() {
return new Person(Math.floor(Math.random() * 100))
}
}
9.ES6类的继承 - extends
- 在ES5中实现继承的方案,虽然最终实现了相对满意的继承机制,但是过程却依然是非常繁琐的。
- 在ES6中新增了使用extends关键字,可以方便的帮助我们实现继承:
class Person { } class Student extends Person { super(); }
10.super关键字
- 现在上面的代码中我使用了一个super关键字,这个super关键字有不同的使用方式:
- 注意:在子(派生)类的构造函数中使用this或者返回默认对象之前,必须先通过super调用父类的构造函数!
- super的使用位置有三个:子类的构造函数、实例方法、静态方法;
// 调用 父对象/父类 的构造函数 super()[arguments]; // 调用 父对象/父类 上的方法 super.functionOnOParent([arguments])
11.继承内置类
- 类继承自内置类,比如Array:
class myArray extends Array {
firstItem() {
return this[0]
}
lastItem() {
return this[this.length -1]
}
}
var arr = new myArray(10, 20, 30)
console.log(arr.firstItem()) // 10
console.log(arr.lastItem()) // 30
12.类的混入mixin
- JavaScript的类只支持单继承:也就是只能有一个父类
- 那么在开发中我们我们需要在一个类中添加更多相似的功能时,应该如何来做呢?
- 这个时候我们可以使用混入(mixin);
class Person { } function mixinRunner(BaseClass) { class NewClass extends BaseClass { running() { console.log("running~") } } return NewClass } function mixinEater(BaseClass) { return class extends BaseClass { eating() { console.log("eating~") } } } // 在JS中类只能有一个父类: 单继承 class Student extends Person { } var NewStudent = mixinEater(mixinRunner(Student)) var ns = new NewStudent() ns.running() ns.eating()