(函数)(变量)在javascript中是什么意思?
我不懂上面代码中的语法。(函数)(变量)在javascript中是什么意思?如何使用这种符号? 如果可能,请给我一些其他示例。(函数)(变量)在javascript中是什么意思?,javascript,Javascript,我不懂上面代码中的语法。(函数)(变量)在javascript中是什么意思?如何使用这种符号? 如果可能,请给我一些其他示例。(函数)(变量)是一种表示生成函数然后立即使用变量调用它的方式。另一种书写方式是: var publisherSubscriber = {}; // we send in a container object which will handle the subscriptions and publishings (function(container) { //
(函数)(变量)
是一种表示生成函数然后立即使用变量调用它的方式。另一种书写方式是:
var publisherSubscriber = {};
// we send in a container object which will handle the subscriptions and publishings
(function(container) {
// the id represents a unique subscription id to a topic
var id = 0;
// we subscribe to a specific topic by sending in
// a callback function to be executed on event firing
container.subscribe = function(topic, f) {
if (!(topic in container)) {
container[topic] = [];
}
container[topic].push({
"id": ++id,
"callback": f
});
return id;
}
// each subscription has its own unique ID, which we use
// to remove a subscriber from a certain topic
container.unsubscribe = function(topic, id) {
var subscribers = [];
for (var subscriber of container[topic]) {
if (subscriber.id !== id) {
subscribers.push(subscriber);
}
}
container[topic] = subscribers;
}
container.publish = function(topic, data) {
for (var subscriber of container[topic]) {
// when executing a callback, it is usually helpful to read
// the documentation to know which arguments will be
// passed to our callbacks by the object firing the event
subscriber.callback(data);
}
}
})(publisherSubscriber);
var subscriptionID1 = publisherSubscriber.subscribe("mouseClicked", function(data) {
console.log("I am Bob's callback function for a mouse clicked event and this is my event data: " + JSON.stringify(data));
});
函数生成后立即调用。它是akaIIFE
(立即调用的函数表达式):
function doSomething(variable) {
...
}
doSomething(variable)