2009年7月30日 星期四

用JavaScript寫物件

<script type="text/javascript">   
employee = function(name, age) {
this.name = name;
this.age = age;
this.talkToYou = function() {
alert("hello I'm " + this.name + " I'm " + this.age + " years old");
}
}
var cloudio = new employee("cloudio", "27");
cloudio.talkToYou();
</script>
employee = function(name,age){...}這邊是JavaScript物件的寫法
而function(name,age)等於建立物件的實例時給的初始值
this.name是物件中的欄位
this.talkToYou = function(){}這是建立物件method的寫法

沒有留言:

張貼留言