在平时的开发中,为了减少多的全局变量和函数,采用面向对象的方式,并且面向对象的编程方法,可以使代码逻辑清晰,代码整洁。
不说了,直接看代码:
var Obj=function(name,age){
this.name=name;
this.age=age;
}
Obj.prototype={
say:function(){
console.log("我的名字是"+this.name,"我的年龄是"+this.age);
},
hello:function(){
console.log("hello world");
}
}
var obj=new Obj("小明",20);
obj.say();//我的名字是小名,我的年龄是20
obj.hello();//hello world
以上是一个简单的面向对象的小例子;
有问题,请留言!!!