constructor函数
不允许以字面量的形式去原型对象上添加属性
class Person{constructor(name,age){this.name = name;this.age = age;}sayName(){console.log(this.name);}}Person.prototype = {sayAge(){console.log(this.age)}}var p = new Person("cheng",20);p.sayAge()
Object.assign
Object.assign可以在原型上添加多个属性
class Person{constructor(name,age){this.name = name;this.age = age;}sayName(){console.log(this.name);}}Object.assign(Person.prototype,{sayAge(){console.log(this.age)},show(){console.log("show")}})var p = new Person("cheng",18);console.log(p.constructor == Person)
模拟栈
栈数据结构的特点:后进先出 例子水杯,谷仓,米袋就是栈的数据结构
1.push
2.pop //将栈顶的数据出栈
3.peek 获取栈顶的数据
4.isEmpty判断栈是否为空
5.size返回栈的长度
location路由 是以栈的数据结构去保存路由的
class Stack{constructor(){this.items = [];}push(value){this.items.push(value)}pop(){return this.items.pop();}peek(){return this.items[this.items.length-1]}isEmpty(){return this.items.length==0;}size(){return this.items.length;}}var s = new Stack();s.push(8);s.push(9);console.log(s.isEmpty())
