-2
我希望能夠像做任何方式來作爲一個函數調用一個對象?
var x = {};
x.something = function(y){
console.log(y);
};
x("hi"); // Call it without using .something
這可能嗎?有任何想法嗎?
我希望能夠像做任何方式來作爲一個函數調用一個對象?
var x = {};
x.something = function(y){
console.log(y);
};
x("hi"); // Call it without using .something
這可能嗎?有任何想法嗎?
var x = function(str) {
return x.something(str);
};
x.something = function(str) {
console.log(str);
};
函數是對象,所以你可以這樣做:
var x = function(y) {
console.log(y);
}
x.prop = function(){ return 'This works'; };
x('hi');
console.log(x.prop());