JavaScript 是一種很強的物件導向的語言,支援創建實例之後再添加屬性和方法,雖然是小技巧,用的時候容易忘記,今天寫了一個很小的例子,記錄在這裡,僅供參考。
function MyClass() { //This function is same as a constructer alert("New Object Created"); } //Creating Object var MyObject = new MyClass (); NewObject.prototype = { //Adding Method named "MyMethod" MyMethod: function(){alert("My Method");} , //Adding property named "MyProperty" MyProperty: "My Property" } //Calling Method MyObject.MyMethod(); //Assigning Property MyObject.MyProperty = "My Property Value changed";