您的位置:首页 > Web前端 > JavaScript

JavaScript面向对象的思想

2010-03-31 21:18 381 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: