JavaScript面向?qū)ο缶幊蹋?)對象上的特殊屬性
來源:程序員人生 發(fā)布時間:2014-12-22 08:55:19 閱讀次數(shù):2770次
js對象都有constructor屬性,但是構(gòu)造函數(shù)產(chǎn)生的對象和簡單對象的屬性有些不同,可自行運行以下代碼了解
<!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⑻" />
<title>對象上的特殊屬性</title>
</head>
<body>
<script type="text/javascript">
function Hero(name) {
this.name = name;
this.occupation = 'Ninja';
this.whoAreYou = function() {
return "I'm " + this.name + " and I'm a " + this.occupation;
}
}
var h1 = new Hero('Michelangelo');
alert(h1.constructor);//返回1個函數(shù)
var obj = {};//var obj = new Object();
alert(obj.constructor);
</script>
</body>
</html>
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈