六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

javascript中prototype用法实例

[复制链接]

升级  66%

9

主题

9

主题

9

主题

童生

Rank: 1

积分
33
 楼主| 发表于 2013-1-26 12:39:33 | 显示全部楼层 |阅读模式
 
<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>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表