Java Script Java Script Object Property Java Script





Java. Script 的物件屬性 Java. Script Object Property � 在 Java. Script 的物件其時就是所為的雜湊(Hash)或是 關聯式陣列(Associative Array)有著鍵與值的對應關係, 物件的定義方式 �Object in Java. Script is so called Associative Array or Hash var hero = { breed: 'Turtle', occupation: 'Ninja' }; � 關聯陣列使用字串來取值(name) �Associative Array use String to get property value. �如: hero. name; // 使用 name 來取值 �或: hero['name']; // 類似其他語言的關聯式陣列取值方法

Java. Script 的物件屬性(續) Java. Script Object Property (Cont. ) �物件裡可以存放另一個物件 �Object can store in Object var book = { name: 'Catch-22', published: 1961, author: { firstname: 'Joseph', lastname: 'Heller' } }; �取值的方式可以使用 Use dot to get property �book. author. firstname;

Java. Script 的物件方法 Java. Script Object Method �在 Java. Script 物件的方法(Method)其實就是儲存 匿名函數(Anonymous Function)的屬性 (Properties) �Put anonymous function in object property become Java. Script object method var hero = { breed: 'Turtle', occupation: 'Ninja', say: function() { return 'I am ' + hero. occupation; } } >>> hero. say(); "I am Ninja"

Java. Script 的物件屬性與方法的修改 Modify object property or method �當物件被建立之後若要修改物件內的屬性或者方法 >>> hero. breed = 'turtle'; >>> hero. name = 'Leonardo'; >>> hero. say. Name = function() {return hero. name; };

使用 this 關鍵字 Keyword ‘this’ �在物件內部時,可以使用 this 關鍵字代表自己、目 前的物件,當物件要使用自己的屬性或方法時便可 以使用 �‘this’ keyword represent object itself var hero = { name: 'Rafaelo', say. Name: function() { return this. name; } } >>> hero. say. Name(); "Rafaelo"

Java. Script 沒有類別 Java. Script no need to define class � 一般常見的物件導向程式,我們會先定義一個類別(Class), 然後透過 new 的方式去實際產生一個物件(Object),可以說 類別是實作物件的一個藍圖 �Generally object is implement of class. � 在 Java. Script 的物件導向中沒有類別的概念,因此 沒有 Class 這個關鍵字可以去定義類別 �Java. Script can implement object directly. � 物件無法透過 Class 定義物件內容,但還是可以透過建構子 的方式達到與類別類似的概念 �Java. Script object through constructor to implement concept of class. �建構子指的是一個函數,當物件產生時(new)會執行 � Constructor is function called to create an object by “new” keyword. �定義一個建構子,在物件產生時把相關的屬性及方法建立起來 � Constructor in Java. Script can define default property and method to alternative class.

建構子 Constructor �建構子可以用來取得建立物件時必要的參數 �Constructor can get necessary arguments when create an object. function Hero(name) { this. name = name; this. occupation = 'Ninja'; this. who. Are. You = function() { return "I'm " + this. name + " and I'm a " + this. occupation; } } >>> var h 1 = new Hero('Michelangelo'); >>> var h 2 = new Hero('Donatello'); >>> h 1. who. Are. You(); "I'm Michelangelo and I'm a Ninja" >>> h 2. who. Are. You(); "I'm Donatello and I'm a Ninja"

建構子與函數的差別 Difference of constructor and function �作為建構子的函數通常不會有傳回值,當然你也可 以使用函數的方式呼叫建構子,它仍然會被執行, 但並無法取得正確的結果 �Although constructor can be called as function. But constructor have no return value. >>> var h = Hero('Leonardo'); >>> typeof h "undefined"


私有函數與公有函數 (續) Private and Public (Cont. ) � 那麼,在建構子當中使用 var 所建立的變數或者函數,則就 可以說是私有的(private),因為出了建構子就無效了。 � The method defined use “var”keyword is public. function Account(bal) { var balance = bal; this. get. Balance = function() { return balance; }; this. deposit = function(money) { if(money > 0) { balance += money; } }; } var account = new Account(1000); account. deposit(500); // OK 私有(Private)屬性或方法 account. get. Balance(); // OK 公有(Public)屬性或方法 account. balance = 1000; // Error

- Slides: 15