1工廠模式 <script> //工廠模式沒建立對象 function createPerson(name,age){ var person = new Object(); person.name = name; person.age = age; person.say = function(){ return "my nanme is "+this.name; } return person; } var person1 = createPerson("lili",20); var person2 = createPerson("lucy",30); print(person1.name); print(person1.age); print(person2.name); print(person2.age); function print(msg){ document.write(msg); document.write("<br>"); } </script> 2構造函數模式 function Person(name,age){ this.name = name; this.age = age; this.say = function(){ return "my nanme is "+this.name; } } var person3 = new Person("jack",20); var person4 = new Person("mike",20); print(person3.name); print(person3.age); print(person4.name); print(person4.age); print(person4.say()); //直接賦值到根window上,window.name至關於name Person("lilei",22); print(name); print(say()); for (var i in window){ print(i+":"+window[i]); } function print(msg){ document.write(msg); document.write("<br>"); }