解题
尝试实现注释部分的Javascript代码,可在其他任何地方添加更多代码(如不能实现,说明一下不能实现的原因):
var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
}
this.waitAndShout = function(){
//隔五秒钟后执行上面的shout方法
}
}
解题:
var Obj = function(msg){
console.log(msg)
this.msg = msg;
this.shout = function(){
alert(this.msg);
}
this.waitAndShout = () =>{
setTimeout(()=>{
this.shout();
},5000)
}
}
var instance= new Obj("message");
instance.waitAndShout();请给JavaScript的String 原生对象添加一个名为trim 的原型方法,用于截取空白字符。要求:
alert(" taobao".trim()); // 输出 "taobao"
alert(" taobao ".trim()); // 输出 "taobao"解题
String.prototype.trim=function(){
var reg=/^\s*([a-zA-Z]{1,})\s*$/
return this.match(reg)[1];
}请编写一段JavaScript脚本生成下面这段DOM结构。要求:使用标准的DOM方法或属性。
<div id=”example”>
<p class=”slogan”>淘!你喜欢</p>
</div>解题
var div=document.createElement("div");
var p=document.createElement("p");
var text=document.createTextNode("淘!你喜欢");
p.appendChild(text);
p.className="slogan";
div.appenChild(p);
div.setAttribute("id","example");
document.body.appenChild(div);
查看4道真题和解析