functionPerson (name, sex) { this.name = name this.sex = sex var evil = '我很邪恶' var pickNose = function () { console.log('我会扣鼻子但不让你看见') } this.drawing = function (type) { console.log('我要画一幅' + type) } } Person.fight = function () { console.log('打架') } Person.prototype.wc = function () { console.log('我是个人我会wc') } var p1 = new Person('lindaidai', 'boy') console.log(p1.name) console.log(p1.evil) p1.drawing('国画') p1.pickNose() p1.fight() p1.wc() Person.fight() Person.wc() console.log(Person.sex)
结果:
1 2 3 4 5 6 7 8 9
lindaidai undefined 我要画一幅画画 Uncaught TypeError: p1.pickNose is not a function UncaughtTypeError: p1.fightisnotafunction 我是个人我会wc 打架 UncaughtTypeError: Person.wcisnotafunction undefined
6. 如果实例自身属性和构造函数原型对象的属性重名
1 2 3 4 5 6 7 8 9
functionCat () { this.color = 'white' this.getColor = function () { console.log(this.color) } } Cat.prototype.color = 'black' var cat = new Cat() cat.getColor() // white
classCat{ constructor (name, color) { var heart = '❤️' var stomach = '胃' var heartbeat = function () { console.log(heart + '跳') } this.name = name this.color = color heartbeat() this.jump = function () { console.log(this) console.log('我跳起来了~来追我啊') } } cleanTheBody = function () { console.log('我会用唾液清洁身体') } static descript = '我这个类是用来生产出一只猫的' static actingCute () { console.log(this) console.log('一听到猫我就想到了它会卖萌') } } Cat.staticName = 'staticName' var guaiguai = new Cat('guaiguai', 'white') // ❤️ 跳 console.log(guaiguai) // Cat { name: 'guaiguai', color: 'white', jump: function() {}, cleanTheBody: function () {} } guaiguai.jump() // Cat { name: 'guaiguai', color: 'white', jump: function() {}, cleanTheBody: function () {} } 我跳起来了~来追我啊 guaiguai.cleanTheBody() // 我会用唾液清洁身体 console.log(guaiguai.descript) // undefined guaiguai.actingCute() // Uncaught TypeError: guaiguai.actingCute is not a function Cat.actingCute() // class Cat{...} 一听到猫我就想到了它会卖萌 console.log(Cat.descript) // 我这个类是用来生产出一只猫的 console.log(Cat.staticName) // staticName
4. 类不存在提升机制
1 2 3 4 5 6 7 8 9
var a = new A() functionA () {} console.log(a) // A {}
var b = new B() classB{} console.log(b) // Uncaught ReferenceError: Cannot access 'B' before initialization
typeof B // function
5. class里面的this指向问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classCat{ constructor () { this.name = 'guaiguai' var type = 'constructor' } type = 'class' getType = function () { console.log(this.type) console.log(type) } } var type = 'window' var guaiguai = new Cat() guaiguai.getType() // class window
调用getType函数的guaiguai, 所以里面的的this指向了guaiguai,而guaiguai的type为class 打印type时,因为没有这个变量,所以往外找,找到了window。 var type = ‘constructor’是函数constructor中的变量, 你也可以理解为是constructor函数的私有变量
6. class里的箭头函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classCat{ constructor () { this.name = 'guaiguai' var type = 'constructor' } type = 'class' getType = () => { console.log(this.type) console.log(type) } } var type = 'window' var guaiguai = new Cat() guaiguai.getType() // class window console.log(guaiguai) // Cat { name: 'guaiguai', type: 'class', getType: function() {}}
如果在构造函数使用了简头函数,this指向的这个实例对象
7. 属性名和方法名相同
1 2 3 4 5 6 7 8 9 10 11
classCat{ constructor () { this.name = 'cat1' } name = 'cat2' getName = function () { console.log(this.name) // cat1 } } var cat = new Cat() cat.getName()
constructor中定义的相同名称的属性和方法会覆盖class里面定义的
8. 再加上原型对象的属性和方法
1 2 3 4 5 6 7 8 9 10 11 12
classCat{ constructor () { this.name = 'cat1' } name = 'cat2' getName = function () { console.log(this.name) } } Cat.prototype.name = 'cat3' var cat = new Cat() cat.getName()
还是以constructor中的为准
9. 箭头函数和方法名相同综合例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classCat{ constructor () { this.name = 'guaiguai' var type = 'constructor' this.getType = () => { console.log(this.type) console.log(type) } } type = 'class' getType = () => { console.log(this.type) console.log(type) } } var type = 'window' var guaiguai = new Cat() guaiguai.getType() // class constructor console.log(guaiguai) // Cat { name: 'guaiguai', getType: function() {}, type: 'class'}