संभव डुप्लिकेट:
How does the (function() {})() construct work and why do people use it?क्यों उपयोग करें (फ़ंक्शन() {....}());
क्यों आधुनिक JavaScript फ़ाइलों की तरह निर्माणों का उपयोग करते हैं:
(function() {
// some real code
}());
अर्थात मैं समझता हूं कि एक अज्ञात फ़ंक्शन बनाया जा रहा है और फिर तत्काल कहा जाता है, बिना पैरामीटर पारित किए जाते हैं ... लेकिन ऐसा क्यों करें और न केवल some real code
पर कॉल करें? और गोल ब्रैकेट की बाहरी जोड़ी क्या है?
विशेष रूप से मैं फ़ाइल js/start.js घूर रहा हूँ Github पर:
(function() {
"use strict";
wooga.castle.GRID_UNIT = 48;
wooga.castle.IMAGES_BASE_URL = "images/entities/";
(function() {
var style = document.createElement('div').style,
prefix;
var candidates = {
webkit: 'webkitTransform',
moz: 'MozTransform', // 'M' is uppercased
ms: 'msTransform',
o: 'oTransform',
'': 'transform'
};
for (var prefix in candidates) {
var candidate = candidates[prefix];
if ('undefined' !== typeof style[candidate]) {
wooga.castle.prefix = prefix;
wooga.castle.prefixedTransform = candidate;
break;
}
}
}());
// XXX why the 2 wrapped "function"s here? XXX
wooga.castle.isNativeWrapper = function() {
var result = !wooga.castle.capabilities.desktop && !wooga.castle.capabilities.android && (! /Safari/.test(navigator.userAgent));
wooga.castle.isNativeWrapper = function() {
return result;
};
return result;
};
}());
मेरी बुनियादी जावास्क्रिप्ट और jQuery कौशल मैं उपरोक्त एकल आदेशों को समझने के साथ
, लेकिन मैं नहीं मिलता है इसलिए वे अंदर लिपटे रहे हैं कई function
एस के। हम सिर्फ कॉल नहीं कर सकता:
"use strict";
wooga.castle.GRID_UNIT = 48;
wooga.castle.IMAGES_BASE_URL = "images/entities/";
var style = document.createElement('div').style,
prefix;
var candidates = {
webkit: 'webkitTransform',
moz: 'MozTransform', // 'M' is uppercased
ms: 'msTransform',
o: 'oTransform',
'': 'transform'
};
for (var prefix in candidates) {
var candidate = candidates[prefix];
if ('undefined' !== typeof style[candidate]) {
wooga.castle.prefix = prefix;
wooga.castle.prefixedTransform = candidate;
break;
}
}
wooga.castle.isNativeWrapper = !wooga.castle.capabilities.desktop && !wooga.castle.capabilities.android && (! /Safari/.test(navigator.userAgent));
मैंने देखा है ((फ़ंक्शन() {})()) 'लेकिन नहीं (फ़ंक्शन() {}())'। – kevin628
@ केविन 628: मैंने कभी नहीं देखा है ((फ़ंक्शन() {})()) '। इस तरह बहुत से '() 's है। दूसरा तरीका यह है: '(फ़ंक्शन() {})() ' –
@ रॉकेट ऐसा लगता है कि वे कम से कम क्रोम में एक ही चीज़ हैं। एक में सिर्फ कम दिमागी झुकाव है() 's। मैंने आज कुछ नया सीखा। – kevin628