JavaScript面向對象的思想

    JavaScript其本身具有強大的面向對象的功能,以下代碼展示了幾種常見的使用方式:

 

<script type="text/javascript">
function object1(name,sex) {
    this.name = name;
    this.sex = sex;
    this.yourname = function(){alert('hello world');};
}


object1.prototype = {
    change:function(name) {alert(name);},
    myname:function(){alert(this.name);}
}


object1.prototype.mysex = function(){alert("sex is"+this.sex);};


obj = new object1('huanghao',1);
obj.myname();
obj.yourname();
obj.change('ha ha');
obj.mysex();

obj2 = {
    name:"huanghao",
    tellname:function(name){alert(name+this.name);}
}
obj2.tellname("hello ");
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章