zoukankan      html  css  js  c++  java
  • javascript常用继承方式.

     
    //原型链继承
    function Parent() {
    this.name = 'per';
    }
    function Child() {
    this.age = 20;
    }
    Child.prototype = new Parent();
    var child = new Child();
    console.log(child.name + " " + child.age)//per 20

    //构造函数继承
     function Parent(age) {
    this.name = 'person';
    this.age = age;
    }
    function Child(age) {
    Parent.call(this, age)
    }
    var child = new Child(20)
    console.log(child.name + " " + child.age)//per 20

    // 组合继承
     function Parent(age) {
    this.name = 'per';
    this.age = age;
    }
    Parent.prototype.sayAge = function () {
    return this.name + ' age is ' + this.age;
    }
    function Child(age) {
    Parent.call(this, age)
    }
    Child.prototype = new Parent();
    var child = new Child(21);
    console.log(child.sayAge())//per age is 21
  • 相关阅读:
    Node.js NPM 包(Package)
    Node.js NPM 作用
    Node.js NPM 介绍
    Node.js NPM 教程
    Node.js NPM 教程
    Node.js 发送Email
    Node.js 上传文件
    Node.js 事件
    Node.js NPM
    Node.js 文件系统模块
  • 原文地址:https://www.cnblogs.com/samsara-yx/p/7873700.html
Copyright © 2011-2022 走看看