JS对象方法,类方法,原型方法

对象方法
function Person(name){
    this.name = name;
    this.Intro = Intro;
}
function Intro(){
console.log(this.name);
}
类方法
Person.run = function(){//不需要创建实例就能调用
    console.log(this.name + " is running.");
}

调用方法:
Person.run();//实例并不能访问这个方法

原型方法
Person.prototype.IntroInterst=function(){
    console.log(this.name+" "+"loves running.");
}

原型方法需要实例化后才能调用。


完整代码

function Person(name){
            this.name = name;
            this.Intro = Intro;//对象方法
        }
function Intro(){
    console.log(this.name);
}

Person.run = function(){//类方法
    console.log(this.name + " is running.");
}

Person.prototype.IntroInterest= function(){


};
var jack = new Person("Jack");//jack没有run方法。
console.log(Object.getOwnPropertyNames(Person));
console.log(Object.getOwnPropertyNames(jack));//jack没有run方法 

Q:类方法的作用是什么?

类似于私有属性。同时var定义的也是私有的属性。


小结:

类方法不需要实例化就可以调用,类似于私有属性or方法。
联想到js的函数有一部分是Object.func(),另一部分是Object.prototype.func()。

联想

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值