Giter VIP home page Giter VIP logo

Comments (1)

xxleyi avatar xxleyi commented on May 27, 2024

one clearer version:

// 定义Person构造函数
function Person (name) {
    this.name = name
    this.sayHello = function(){
        console.log('hello from person')
    }
}

Person.prototype = new Person()
Person.prototype.constructor = Person
Person.prototype.sayName = function(){
    console.log(this.name)
}

// 定义Teacher构造函数
function Teacher (name,level){
    this.name = name
    this.level = level
}
// Teacher.prototype = new Person()
// Teacher.prototype.constructor = Teacher
Teacher.prototype = new Teacher()
Teacher.prototype.__proto__ = Person.prototype
Teacher.prototype.constructor = Teacher
Teacher.prototype.sayLevel = function(){
    console.log(this.level)
}

// 定义MathTeacher构造函数
function MathTeacher(name, level, className){
    this.name = name
    this.level = level
    this.className = className
}
// MathTeacher.prototype = new Teacher()
// MathTeacher.prototype.constructor = MathTeacher
MathTeacher.prototype = new MathTeacher()
MathTeacher.prototype.__proto__ = Teacher.prototype
MathTeacher.prototype.constructor = MathTeacher
MathTeacher.prototype.sayClassName = function (){
    console.log(this.className)
}

let liSi = new Person('Li Si')
let wangLaoShi = new Teacher('Wang Wu','Normal')
let zhangLaoShi = new MathTeacher('Zhang San','Great', 'Math')


console.log(liSi.hasOwnProperty("sayHello"))
console.log(wangLaoShi.hasOwnProperty("sayHello"))
console.log(zhangLaoShi.hasOwnProperty("sayHello"))

liSi.sayHello()
wangLaoShi.sayHello()
zhangLaoShi.sayHello()


liSi.sayName()
wangLaoShi.sayName()
zhangLaoShi.sayName()

wangLaoShi.sayLevel()
zhangLaoShi.sayLevel()

zhangLaoShi.sayClassName()

JS Tutor 演示

from learning_list.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.