首页 > 编程语言 > 详细

javascript原型和继承

时间:2020-03-20 11:16:50      阅读:47      评论:0      收藏:0      [点我收藏+]

一、原型

 

二、继承

function Person(name){
    this.name=name;
}
Person.prototype.eat=function(){
    return "吃饭";
}

function Student(name,sex){
    Person.apply(this,[name]);
    this.sex=sex;
}
Student.prototype=Object.create(Person.prototype);
Student.prototype.study=function(){
    return "学习";
}

var student=new Student("小明","");
console.log(student.name);
console.log(student.sex);
console.log(student.eat());
console.log(student.study());

1、构造函数的属性放在对象上,函数放在原型上;

2、继承属性【Person.apply(this,[name]);】,继承方法【Student.prototype=Object.create(Person.prototype);】;

 

javascript原型和继承

原文:https://www.cnblogs.com/linding/p/12530365.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!