标签:全局变量 对象 javascript
<span style="font-size:14px;"> function Dog(){ this.wow = function(){ alert("Wow"); } this.yelp = function(){ this.wow(); } }</span>小蟒和小贤一样,原来也是一条可爱的小狗,可是突然有一天疯了(MadDog),一看到人就会每隔半秒叫一声(wow)的不停叫唤(yelp)。请根据描述,按示例的形式用代码来实现(提示:继承,原型,setInterval)
<span style="font-size:14px;">function MadDog(){ this.yelp = function(){ var self = this; //继承 setInterval(function(){ self.wow(); },500); } } MadDog.prototype = new Dog(); //声明原型 var dog = new Dog(); dog.yelp(); var maddog = new MadDog(); maddog.yelp();</span>当然,我们也可以不用到继承或者原型的方法去实现,那么我们就可以这样:
<span style="font-size:14px;"> var yelp = function(){ alert("wow"); var wow = setTimeout(function(){ yelp(); },500); } yelp();</span>直接调用方法,也可以直接实现
<span style="font-size:14px;"><body> <!--之前浏览界面--> <a href="test1.html">跳转</a> </body> <body> <!--登陆界面--> <form> <input type="text" id="username"> </form> <button id="btn" onclick="demo()">提交</button> <script> function demo() { var name = document.getElementById("username").value; if (name == "L") { history.go(-1); } else { alert("用户名输入错误,请重新输入!"); } } </script> </body></span>
<span style="font-size:14px;">location.assign("http:\\www.baidu.com");</span>
<span style="font-size:14px;">/** * Created by pc on 2015/8/27. */ (function() { function person() { this.name = name; } person.prototype.say = function () { //原型 alert("Hello"); } window.person = person; //信息调用,闭包内执行 }()); //信息封装 function Student(){ this.name = name; } /*子类调用父类方法*/ Student.prototype = new person(); //继承 /*父类回调子类方法*/ var per = Student.prototype.say; Student.prototype.say = function(){ per.call(this); alert("stu-Hello"); } var stu = new Student(); stu.say(); </span>书写方式TWO
<span style="font-size:14px;">/** * Created by pc on 2015/8/27. */ (function() { function person() { var _this = {}; _this.name = name; _this.say = function(){ alert("Hello"); } return _this; } window.person = person; }()); //信息封装 function Student(){ var _this = person(); /*父类回调子类方法*/ var per = _this.say; _this.say = function(){ per.call(this); alert("per-Hello"); } return _this; } /*子类调用父类方法*/ var stu = new Object(); stu.say(); </span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
奔跑吧,我的JavaScript(3)---JavaScript浏览器对象
标签:全局变量 对象 javascript
原文地址:http://blog.csdn.net/u011318165/article/details/48036409