是否可以做这样的事情?
var foo;
foo.bar = function { };
foo.variable1 = "Hello World";
foo.variable2 = 5;
......并以某种方式使我能够:
function example() {
foo(); // <-- have this call foo.bar()
foo.variable1 = "Goodbye World"; // <-- but still have this work
}
答案 0 :(得分:3)
烨。函数也只是对象。
var foo = function () { alert("Hi!") };
foo.variable1 = "Hello world";
console.log(foo.variable1);
foo();
答案 1 :(得分:0)
比你想象的容易。
var foo = function() {
foo.bar();
};
foo.bar = function() {
console.log('Hello World. '+ foo.variable1);
};
foo.variable1 = "Goodbye World";
foo(); // "Hello World. Goodbye World"