Prince_fmx的博客通过本文主要向大家介绍了前端面经汇总,静态方法 实例方法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
静态方法(属性)
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();