实例方法与原型上的方法

function test(name){
  this.name = name;
  this.say = function(){
    console.log('say');
    
  }
  this.getProtoName = function(){
    return test.prototype.name
  }
}

// 设置原型上的方法和属性
test.prototype.name='prototype name'
test.prototype.say= function(){
  console.log('prosay');
}
test.prototype.jump= function(){
  console.log('jump');
}
var t = new test('test')
  • 测试
t.say()// ‘sya'  自己有这个方法,原型上也有,此时调用自己的
t.jump() // 'jump' 自己没有这个方法,调用原型上的
t.getProtoName() // 'prototype name' 获取原型上的name属性
test.prototype.say() // 'prosay' 直接调用原型上的

猜你喜欢

转载自blog.csdn.net/qq1498982270/article/details/90137964
今日推荐