在 JavaScript 裡,constructor() 屬性會回傳一個指向建立該物件實例的建構函式。簡單說,它能告訴你「這個物件是被哪個函式所創造出來的」。
boolean.constructor()
回傳建立此物件的建構函式。
<html>
<head>
<title>JavaScript constructor() Method</title>
</head>
<body>
<script type="text/javascript">
var bool = new Boolean( );
document.write("bool.constructor() is:"+bool.constructor);
</script>
</body>
</html>執行後的結果如下:
bool.constructor() is : function Boolean() { [native code] }從輸出可以看到,bool 這個物件是由原生的 Boolean() 建構函式所建立。constructor 屬性在判斷物件型別或進行繼承相關操作時相當有用。