JavaScript学习----Prototype
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JavaScript Study 2015.11.9--</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> </style> <script type="text/javascript"> //prototype的内存分析 /* //第1种状态 function Person(name,age){ this.name = name; this.age = age; this.say = function(){ alert(this.name+","+this.age); } } //第2种状态 Person.prototype.sayHi = function(){ alert(this.name+":sayHi"); } //第3种状态 var p1 = new Person("lili",20); //第4种状态 p1.say(); */ //prototype重写的内存分析 //第1种状态 function Person(name,age){ this.name = name; this.age = age; this.say = function(){ alert(this.name+","+this.age); } } //第2种状态 var p1 = new Person("lili",20); //第3种状态 Person.prototype = { sayHi:function(){ alert("sayHi"); } } //第4种状态 var p2 = new Person("disk",30); p1.say();//no error p2.say();//no error //p1.sayHi();//error p2.sayHi();//no error </script> </head> <body> <div id="wrap"> </div> </body> </html>