手记

Javascript中prototype和方法封装的一些知识

javascript中,prototype的作用主要是为了为类添加方法。


<script language="javascript">

//定义一个类
var Person = function(name,age){
              this.name = name;
              this.age = age;
              this.sayHello=function(){
                   //this可以省略
                  alert("hello," + this.name);
              }
};

//为类添加一个方法,this不能省略!
Person.prototype.talk = function(){
    alert(this.name + ":" + this.age +":"+ "talk");
};

var zhangsan = new Person("zhangsan",19);
var lisi = new Person("lisi",19);
zhangsan.talk();
zhangsan.sayHello();
lisi.talk();
lisi.sayHello();

var xinqushi = XINQUSHI = {
    sayHello: function(name){
        alert("hello," + name);
    }
}

TT.sayHello("girl!");
TAOTAO.sayHello("boy!");

</script>


0人推荐
随时随地看视频
慕课网APP