我正在尝试在警报窗口中打印出person对象的名称值。
<div class="col-md-6 text-right">
<h2 class="table-section-heading text-center">Premium Listings</h2>
<ul class="list-group">
<li class="list-group-item list-group-item-success"><span style="font-size:26px" class="alink"><a href="#">50% Off Winter All Coats #Macys </a></span><span style="font-size:16px"> <i>(ad)</i></span></li>
</ul><ul class="list-group">
<li class="list-group-item list-group-item-info"><span style="font-size:26px"><a href="#">Table Put Together</a></span><span style="font-size:16px"> in <i><span class="label label-info"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> Beverly Hills, CA</span></i></span> - <b><span class="label label-success">$54.00</span></b></li>
</ul><ul class="list-group">
<li class="list-group-item list-group-item-info"><span style="font-size:26px"><a href="#">Dog Sitter</a></span><span style="font-size:16px"> in <i><span class="label label-info"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> Hudson, OH</span></i></span> - <b><span class="label label-success">$92.00</span></b></li>
</ul><ul class="list-group">
<li class="list-group-item list-group-item-info"><span style="font-size:26px"><a href="#">Shopping</a></span><span style="font-size:16px"> in <i><span class="label label-info"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> Hudson, OH</span></i></span> - <b><span class="label label-success">$150.00</span></b></li>
</ul><ul class="list-group">
<li class="list-group-item list-group-item-info"><span style="font-size:26px"><a href="#">Driver</a></span><span style="font-size:16px"> in <i><span class="label label-info"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> Valencia , CA</span></i></span> - <b><span class="label label-success">$45.00</span></b></li>
</ul><ul class="list-group">
<li class="list-group-item list-group-item-info"><span style="font-size:26px"><a href="#">Receptionist for a day</a></span><span style="font-size:16px"> in <i><span class="label label-info"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> Chicago, IL</span></i></span> - <b><span class="label label-success">$80.00</span></b></li>
</ul><table class="table">
</table>
</div>
<!-- /Premium listings table -->
<!-- Jobs near you table -->
<div class="col-md-6" style="float:right;">
<h2 class="table-section-heading text-center">Near you</h2>
<ul class="list-group">
<li class="list-group-item list-group-item-info premium"><span style="font-size:26px" class="text-nowrap"><a href="#">Table Put Together</a></span><span style="font-size:16px"> in <i><span class="label label-info"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> Beverly Hills, CA</span></i></span> - <b><span class="label label-success">$54.00</span></b></li>
</ul><ul class="list-group">
<li class="list-group-item list-group-item-info standard"><span style="font-size:26px" class="text-nowrap"><a href="#">This list long. Too long. Looking for driver.</a></span><span style="font-size:16px"> in <i><span class="label label-info"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> Beverly Hills , CA</span></i></span> - <b><span class="label label-success">$85.00</span></b></li>
</ul><ul class="list-group">
<li class="list-group-item list-group-item-info premium"><span style="font-size:26px" class="text-nowrap"><a href="#">Driver</a></span><span style="font-size:16px"> in <i><span class="label label-info"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> Valencia , CA</span></i></span> - <b><span class="label label-success">$45.00</span>
</div>
我得到的结果是一个空的警告框。当我在名称中硬编码时,警报框正确打印。我试过say_name:alert(person.name),但返回一个未定义的。我做错了什么?
答案 0 :(得分:1)
您需要为say_name
分配一个功能。现在,您立即调用该函数并分配其undefined
返回值。这使得this
引用了除你期望之外的东西。
var person = {
name: 'Tray',
distance_travelled: 0,
say_name: function() {
alert(this.name);
},
}
person.say_name();
现在this
arg将引用person
对象,因为当您执行some_object.someMethod()
时,this
中someMethod()
的值会自动设置为调用它的some_object
。