ES6语法

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>Document</title>
<script>
function Person(name, sex, age){
this.name = name;
this.sex = sex;
this.age = age;
}
Person.prototype.showSelf = function(){
alert(`我是一个叫${this.name},今年${this.age}岁的${this.sex}孩`);
}
//白领人
function Worker(name, sex, age, job){
//1、构造函数的伪装 -- 继承父级的属性
Person.call(this, name, sex, age);
this.job = job;
}
//2、原型链 继承父一级的方法
//<1>通过for...in遍历继承
for(var funcName in Person.prototype){
Worker.prototype[funcName] = Person.prototype[funcName];
}
//<2>Object.create()
// Worker.prototype = Object.create(Person.prototype);
//<3>调用构造函数继承
Worker.prototype = new Person();
Worker.prototype.showJob = function(){
alert("我的工作是" + this.job);
}
var w1 = new Worker("小米", "男", 20, "程序员");
w1.showSelf();
w1.showJob();

//ECMA6class语法
// class Person{
// //class属性添加
// constructor(name, sex, age){
// this.name = name;
// this.sex = sex;
// this.age = age;
// }
// showSelf(){
// alert(`我是一个叫${this.name},今年${this.age}岁的${this.sex}孩`);
// }
// }
//
// var p1 = new Person("blue", "男", 18);
// p1.showSelf();
//
// //extends 继承
// class Worker extends Person{
// constructor(name, sex, age, job){
// //1、继承到父一级的属性
// super(name, sex, age);
// this.job = job;
// }
// showJob(){
// alert("我的工作是" + this.job);
// }
// }
//
// var w1 = new Worker("小米", "男", 20, "程序员");
// w1.showSelf();
// w1.showJob();
</script>
</head>
<body>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值