當我使用String構造函數時,我總是想知道它是如何製作的。它返回的字符串作爲參數指定,儘管它與「new」運算符類似:構造函數返回值不是「[Object object]」
var str = new String("Hello World")
//str == "Hello World"
如果我做一個構造函數,我給它返回一個返回值的對象實例化時:
function CreateString(st) {
return ''+st
}
var MyString = new CreateString("Hello there!")
//MyString == "[Object object]"
我怎樣才能創建一個返回除此之外值的構造函數[Object對象] ?
更新: 我試圖創建一個具有兩個屬性的值等本身就是一個構造函數:
function MyConstructor() {
this.myMethod = function() {
return 73
}
return 25
}
/*I want MyConstructor to have the value 25 and the myMethod method */
alert(MyConstructor()) // to be 25
alert(MyConstructor().myMethod()) //to be 73
有非常,非常少的原因,以創建String實例。 – Pointy
你想做什麼?創建一個新的'類'或只是創建一個對象以外的東西? – Shilly