-1
在JavaScript中,我可以调用对象,例如; String
,Number
和Boolean
有两种不同的方式,一种是标准函数调用,另一种是构造函数。JavaScript:函数调用vs构造函数
例
// Call String as a standard function call.
var a = String("Hello World");
// a = "Hello World";
// Call String as a constructor function.
var b = new String("Hello World");
// b = String {0: "H", 1: "e", 2: "l", 3: "l", 4: "o", 5: " ", 6: "W", 7: "o", 8: "r", 9: "l", 10: "d"}
比方说我现在设计自己的对象; Foo
var Foo = (function() {
function Foo(value) {
// Construct a foo object
}
return Foo;
})();
// Test
var foo = new Foo();
好了,我可以叫Foo
作为一个构造函数,但如何实现Foo
作为标准函数调用,就像这样
var foo = Foo(myValue);