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

JS面向对象的写法

2012-06-25 17:08 281 查看



JS面向对象的标准写法 收藏

view plaincopy to clipboardprint?

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>新建网页 1</title>

<mce:script type=text/javascript><!--

var person=function(name,age){//定义对象构造方法

this.name=name;

this.age=age;

}

person.prototype={ //封装方法

getName:function(){

alert(this.name);

},

getAge:function(){

alert(this.age);

}

}

function test(){//声明调用

var man=new person('jack',12);

man.getName()

man.getAge()

}

var test2 ={ //类似静态方法 调用直接:test2.te();即可

te:function(){

alert();

},

te1:function(){

alert();

}

}

// --></mce:script>

</head>

<body>

<input type=button onclick="test()"/>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: