function Dog() { this.wow = function() { alert(’Wow’); } this.yelp = function() { this.wow(); } }小芒和小贤一样,原来也是一条可爱的小狗,可是突然有一天疯了(MadDog),一看到人就会每隔半秒叫一声(wow)地不停叫唤(yelp)。请根据描述,按示例的形式用代码来实现(提示关键字: 继承,原型,setInterval)。
this.yelp = function() { var self = this; setInterval(function() { self.wow(); }, 500); } } MadDog.prototype = new Dog(); //for test var dog = new Dog(); dog.yelp(); var madDog = new MadDog(); madDog.yelp();
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="Keywords" content="简单的XHTML页面" /> <meta name="Description" content="这是一个简单的XHTML页面" /> <title>简单的XHTML页面</title> </head> <body> <style type="text/css"> .box { display: table-cell; vertical-align:middle; width:200px; height:200px; text-align:center; /* hack for ie */ *display: block; *font-size: 175px; /* end */ border: 5px solid red; } .box img { vertical-align:middle; } </style> <div class="box"> <img src="http://justinyoung.cnblogs.com/images/cnblogs_com/justinyoung/2007/tb_m.PNG" alt="" /> </div> </body> </html>
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="Keywords" content="简单的XHTML页面" /> <meta name="Description" content="这是一个简单的XHTML页面" /> <title>简单的XHTML页面</title> </head> <body> <div style="height:1px;overflow:hidden;background:#000"></div> </body> </html>
Array.prototype.distinct = function() { var ret = []; for (var i = 0; i < this.length; i++) { for (var j = i+1; j < this.length;) { if (this[i] === this[j]) { ret.push(this.splice(j, 1)[0]); } else { j++; } } } return ret; } //for test alert(['a','b','c','d','b','a','e'].distinct());
淘宝招聘官评语:这是最为普通的解法。在数组元素数量不高的情况下,它的性能是可以接受的。相信一定有不少朋友有更好的解法,请告诉我们。