原型链
递归
在一个函数内部调用自身
function fun(){
fun()
}
递归:在函数内部调用自己
使用递归:一定要有退出条件
var n = 1; function fun() { if (n > 6) return; console.log("我喜欢写代码") n++ fun() }
fun()
闭包:
闭包定义:
有权访问另一个函数作用域中的变量的函数
简单理解: 一个作用域可以访问另一个函数内部的变量
面向对象编程
es5实现
function Star(uname, height, weight) { this.uname = uname; this.height = height; this.weight = weight this.say = function () { console.log(`我是${this.uname},我身高${this.height}厘米,体重${this.weight}公斤`) } } var ldh = new Star("刘德华", 174, 64) ldh.say() var lyf = new Star("刘亦菲", 170, 80) lyf.say() 定义一个构造函数Circle,里面有两个方法area和length,分别计算圆的面积和周长
ES6实现
//定义一个类 class Star { //构造函数 constructor(uname, height, weight) { //this指向实例化对象 this.uname = uname; this.height = height; this.weight = weight; } say() { return `我是${this.uname},身高${this.height}厘米,体重${this.weight}公斤` } } var ldh = new Star("刘德华", 185, 80) console.log(ldh.say())
定义一个构造函数Circle,里面有两个方法area和length,分别计算圆的面积和周长
class Circle { constructor(r) { this.r = r } //计算周长 length() { return (2 * Math.PI * this.r).toFixed(2) } //计算面积 area() { return (Math.PI * r ** 2).toFixed(2) } } console.log(new Circle(4).length()) console.log(new Circle(4).area())
ES6继承
// 父类 Person class Person { constructor(uname, age) { //属性 this.uname = uname; this.age = age } //方法 say() { return `我是${this.uname},我年龄${this.age}岁` } } //子类Student class Student extends Person { constructor(uname, age) { // this.uname = uname; // this.age = age; super(uname, age) } } var s1 = new Student("小红", 18) console.log(s1.say())