javascript中prototype用法实例
<html><head><script>var TestReturn = function() {var abc = 0;return {setAbc : function(c) {abc = c;},add : function(a, b) {return abc == 0 ? a + b : abc + a + b;}}}var Test = function(name) {this.name = name;this.sayHello = function() {alert("sayHello " + this.name);}}Test.prototype = new TestReturn();Test.prototype.lovePro = function() {alert("test.prototype.lovePro : love " + this.name);}Test.run = function() {alert("run");}var test1 = new Test("test1");var test2 = new Test("test2");test1.love = function() {alert("test1.love : love");}test1.setAbc(10);alert(test1.add(1, 2)); //13alert(test2.add(1, 2)); //13test1.sayHello();test2.sayHello();test1.love();Test.run();test1.lovePro();test2.lovePro();//test1.run();错误//test2.love(); 错误//test1.prototype.lovePro = function() {//alert("test1.prototype.lovePro : love");//} 错误</script></head><body></body></html>
页:
[1]