您的位置:首页 > Web前端

静态方法(属性)与实例方法(属性)

2017-09-04 17:56 357 查看
静态方法(属性)

1,静态方法(属性)是不需要实例化对象用类名就能直接调用的方法

2,静态方法(属性)定义是在类的外部用类名定义的,不能在内部定义,调用的时候直接用类名调用;

3,静态方法(属性)不能被实例对象调用

代码:

function Animal(){

}
Animal.counts=100;//静态属性
Animal.Cat=function(){//静态方法
console.log("this is a cat!.");
}
console.log(Animal.counts);
Animal.Cat();//this is a cat!

var Animal2=new Function;
Animal2=Animal;
Animal2.Cat();//this is a cat!

实例方法(属性)

1,实例方法是在类的内部用this定义的,或者在外部用prototype定义的,

2,而且要创建实例对象才可以调用实例方法

代码:

function Person(){
this.man=function(){
console.log("this is a man.");
}
}
Person.prototype.woman=function(){
console.log("this is a woman.");
}
var person=new Person();
person.man();
person.woman();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐