下面代码是我在看了某个贴子后有点疑问修改的,请大家指点一下,代码1,2的优缺点,使用场景,谢谢!代码1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>New Document </title> <script type="text/javascript">
function Person(name) {
this.name = name;
} Person.prototype.showName = function() {
alert(this.name);
} Person.prototype.say = function() {
setTimeout(function() { this.showName.call(this) }, 1000);
} var p = new Person("我");
p.say();
</script></head>
<body>
</body>
</html>代码2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>New Document </title> <script type="text/javascript">
function Person(name) {
this.name = name;
} Person.prototype.showName = function() {
alert(this.name);
} Person.prototype.say = function() {
var othis = this;
setTimeout(function() { othis.showName() }, 1000);
} var p = new Person("我");
p.say();
</script></head>
<body>
</body>
</html>
<html>
<head>
<title>New Document </title> <script type="text/javascript">
function Person(name) {
this.name = name;
} Person.prototype.showName = function() {
alert(this.name);
} Person.prototype.say = function() {
setTimeout(function() { this.showName.call(this) }, 1000);
} var p = new Person("我");
p.say();
</script></head>
<body>
</body>
</html>代码2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>New Document </title> <script type="text/javascript">
function Person(name) {
this.name = name;
} Person.prototype.showName = function() {
alert(this.name);
} Person.prototype.say = function() {
var othis = this;
setTimeout(function() { othis.showName() }, 1000);
} var p = new Person("我");
p.say();
</script></head>
<body>
</body>
</html>
setTimeout(function() { this.showName.call(this) }, 1000);
} Person.prototype.say = function() {
var othis = this;
setTimeout(function() { othis.showName() }, 1000);
}
这两个就是找当前作用域的方式不同而已~·
至于那个好些,个人觉得完全是自己习惯哪种就用哪种。
function showName()
{}
function Person(name) {
this.name = name;
} Person.prototype.showName = function() {
alert(this.name);
} Person.prototype.say = function() {
alert(this instanceof Person)
setTimeout(function() { /*this.showName.call(this)*/alert(this instanceof Person) }, 1000);
} var p = new Person("我");
p.say();
可以看看,在setTimeout方法的参数里,this表示的并不是你想要的那个对象。
Person.prototype.say = function() {
var othis = this;
setTimeout(function() { othis.showName() }, 1000);
}
第一种写法明显是错的 不提出this指代 在setTimeout函数中 的this指代的是window 而不是Person对象 this.showName会报错 因为window压根就没定义过这个方法第二种 将this提出 这个作用域就变了 指向Person对象 调用就没问题了
Person.prototype.showName.call(this)
这样呢.和代码2相比较呢.
主要就是比较用var othis = this和 call两种方法
假如在某情况下,我可以用call这种,也可以用var othis = this.
我应该怎么选择,为什么?
在有些时候,this表示的可能不是我们需要的对象。
比如你的代码一。在javascript里,当函数以函数的方式调用的时候,函数里的this指向的是window对象。就像aspwebchh说的。这时候再使用call方法就行不通了。
至于该用什么没想过,反正能让this指向正确的对象就好了。
setTimeout(this.sayHello.call(this), 1000);
wo 想楼主第一段代码应该这样写吧
而代码一是函数调用的四种方法之一。call有个更常用的替代者apply()。
不知道我说明白没?囧~~