JavaScript-封装与继承(两种)


构造函数的方法

一、封装

1.1命名空间

 // 命名空间:把变量当做属性,本质上是字面量对象
  let zsf = {
			uname : '李四',
			age : 22,
			email : 'zsf@qq.com'
		}
  let lxh = {
			uname : '张三',
			age : 21,
			email : 'lxh@qq.com'
		}

		let zwj = zsf;
		zwj.uname = '王五'
console.log( zsf.uname, lxh.uname );
		
//缺点是 谁封装完只能谁使用,别人不能使用

在这里插入图片描述

1.2构造函数

 // 构造函数:把构造函数当做模板
    function Person(uname, age) {
      this.uname = uname;
      this.age = age;
      this.eat = function () {
        console.log("方法");
      };
    }
    let zsf = new Person("haha", 22);
    console.log(zsf);

1.3原型对象

 // 在原型上的成员(属性 和 方法) 都是共享给实例对象的,
    // _proto_,(原型)指向原型对象,每个对象都有的属性,非标准属性
    function Person(uname, age) {
      this.uname = uname;
      this.age = age;
      // this.eat = function () {
      //   console.log("方法");
      // };
    }
    Person.prototype.eat = function () {
      console.log("原型");
    };
    let obj = new Person("haha", 22);
    console.log(obj);

二、继承

原型继承

 function Person() {
      this.arms = 2;
      this.legs = 2;
      this.eyes = 2;

      // 人的行为
      this.walk = function () {};
      this.sing = function () {};
      this.sleep = function () {};
    }
    function Chinese() {
      // 中国人的特征
      skin = "yellow";
      language = "中文";
    }

    // 封装日本人的行为特征
    function Japanese() {
      // 日本人的特征
      skin = "yellow";
      language = "日语";
    }
    // 1.给原型对象继承
    // 2.指回去
    Chinese.prototype = new Person();
    Chinese.prototype.constructor = Chinese;

    Japanese.prototype = new Person();
    Japanese.prototype.constructor = Japanese;
    // console.log(p);
    let c1 = new Chinese();
    console.log(c1);
    let c2 = new Japanese();
    console.log(c2);

在这里插入图片描述

原型链

在这里插入图片描述


类的方法

一、封装

创建类:

 // 创建类
  class Person {
	// 此处编写封装逻辑
	name='小红'      //实例成员
	static age=22   //静态成员
  }

  // 实例化
  let p1 = new Person();
  console.log(p1);

构造封装函数:

  class Person {
    // 实例化时 立即执行
    // 构造函数、构造方法、构造器
    constructor (name, age) {   //并非是类中必须要存在的方法
      this.name = name;
      this.age = age;
    }
	// 实例方法
    walk () {
      console.log(this.name + '走路');
    }
  }
	
  // 实例化
  let p1 = new Person('小明', 18);
  p1.walk();

二、继承

2.1 extends

  class Person {
    // 父类的属性
    legs = 2;
    arms = 2;
    eyes = 2;
		// 父类的方法
    walk () {
      console.log('走路');
    }
		// 父类的方法
    sleep () {
      console.log('睡觉');
    }
  }

  // Chinese 继承了 Person 的所有特征
  class Chinese extends Person {}

  // 实例化
  let c1 = new Chinese();
  c1.walk();

2.2 super(在继承的过程中子类中 constructor 中必须调 super 函数,否则会有语法错误)

class Father {
				constructor (uname, age) {
					this.uname = uname;
					this.age = age;
				}

				qian () {
					console.log('哈哈哈');
				}
			}
			// 如果子类有自己的constructor,那么必须使用super调用父类的方法
			// super:调用父类的方法
			class Son extends Father {
				constructor (u, a, score) {
					super(u, a);// 必须先调用super
					this.score = score;
				}

				qian () {
					console.log('嘻嘻嘻');
					super.qian();
				}
			}


			let s = new Son('儿子', 23, 99);

			console.log( s );

			s.qian();

以上这种情况就是说,子类有自己单独的属性或者方法,所以子类有自己的constructor,那么必须使用super调用父类的方法

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值