मैं हाल ही में console.log.toString()
फोन करके firebugs console.log
के कोड में देखा और यह मिल गया: जब तक मैं समझता हूँ कि इस का कारण बनता है Function.apply
अपने this
साथ कहा जाFunction.apply बनाम Function.prototype.apply
function() { return Function.apply.call(x.log, x, arguments); }
x.log
का जिक्र है और x
और arguments
के तर्क हैं। चूंकि Function.apply
स्वयं फ़ंक्शंस कॉल करता है, जिसके परिणामस्वरूप x.log
को इसके this
के साथ x
और arguments
का तर्क दिया गया है।
जो मुझे मेरे प्रश्न पर ले जाता है: क्या Function.apply
पर कॉल करने के लिए Function.prototype.apply
का उपयोग करने का कोई कारण है? या दूसरे शब्दों में, ऊपर और return x.log.apply(x, arguments)
के बीच कोई अंतर है?
संपादित करें: यह खुला स्रोत मैं फ़ायरबग sourcecode का शीघ्रता से अवलोकन लिया और जहां इस बनाई गई है जगह मिल (consoleInjector.js, रेखा 73) है के बाद से:
// Construct a script string that defines a function. This function returns
// an object that wraps every 'console' method. This function will be evaluated
// in a window content sandbox and return a wrapper for the 'console' object.
// Note that this wrapper appends an additional frame that shouldn't be displayed
// to the user.
var expr = "(function(x) { return {\n";
for (var p in console)
{
var func = console[p];
if (typeof(func) == "function")
{
expr += p + ": function() { return Function.apply.call(x." + p +
", x, arguments); },\n";
}
}
expr += "};})";
// Evaluate the function in the window sandbox/scope and execute. The return value
// is a wrapper for the 'console' object.
var sandbox = Cu.Sandbox(win);
var getConsoleWrapper = Cu.evalInSandbox(expr, sandbox);
win.wrappedJSObject.console = getConsoleWrapper(console);
मैं अब लगभग यकीन है कि Function
के साथ यह कुछ अलग-अलग दायरे में होने के लिए कुछ है, जो मैंने अपनी पहली टिप्पणी में pst के उत्तर में कहा है, लेकिन मैं अभी भी इसे पूरी तरह से समझ नहीं पा रहा हूं। मैं इसके बारे में थोड़ा और शोध कर सकता हूं।
मुझे उत्सुकता है कि क्यों कोड मानक बंद करने के दृष्टिकोण में 'Cu.evalInSandbox ("फंक्शन", सैंडबॉक्स) के परिणाम का उपयोग नहीं कर रहा है (क्योंकि फ़ंक्शन-ऑब्जेक्ट्स स्पष्ट रूप से लौटाए जाते हैं), लेकिन स्पष्ट रूप से वहां एक कारण है .. असल में, यह तब से ज्यादा नहीं बनाता है, क्योंकि 'फंक्शन' को बंद नहीं किया जाना चाहिए; जब तक 'evalInSandbox' में और अधिक जादू न हो .. –