Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm developing a website with jQuery and organize code into several script files.
If I didn't get it wrong, there's a best practice of organizing your code like this boilerplate (which creates a separated scope and set the undefined value correctly):
(function($, undefined) {
$(document).ready(function() {
// code here
});
})(jQuery);
In my case, I will also rely on some external configuration outputted by some server-side script, say it is stored as global variable site_config. So currently my script boilerplate is:
(function(window, undefined) {
var document = window.document;
var $ = window.jQuery;
var config = window.site_config;
// equivalent to $(document).ready(function() {...
$(function() {
// code here using `config`
});
})(window);
So my question is: is there any catch/gotcha in this boilerplate?
Specifically:
is it OK to pass in window explicitly? (suppose the wrapper function is in global scope)
should I use window.document or jQuery.document? If both are wrong, how to reference correctly?
is it OK to pass in window explicitly?
It's OK, but unnecessary (see Why do I pass the window object in to the module pattern?).
should I use window.document or jQuery.document? If both are wrong, how to reference correctly?
jQuery.document is wrong, the jQuery function object has no such property. However, it would be fine to just use document, as it is a global and non-writable variable.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
While in VS Code, I hit the tab after typing return and the VS Code Intellisense auto-completed "return" to "this.return".
I've never seen this before and cannot find any documentation on it.
Does anybody know if this is actually a thing, or if VS Code possibly lost it's marbles.
Note: the object I was working in does not have any properties or functions called "return".
Well, an object could have a property called return:
const obj = {
return: () => { console.log('↩️'); }
};
obj.return();
Were you in the context of a class that had a return property, or maybe its superclass did?
(Honestly it seems more likely VS Code was just being weird.)
You can run console.log("this", this) in most modern browsers this will return the JSON data of this (the variable this refers to the current instance of declared object FYI), and console.log(typeof(this.return)) and it will likely return function (if you get undefined just change it from this.return to return;)
Likely the object either has a property called return that is a function, or something has gone wrong in the autocomplete.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
var main = function() {
this.first = this;
}
console.log(new main().first);
The code is here :
Object is creating recursively ,I didnot understand why the console not throwing any error.
Please tell me if there is any concept behind this.
You are making it circular (not recursive) with this:
this.first = this
It's the same as if you do:
var obj = {}
obj.first = obj
obj.first will reference obj. That in javascript is like saying obj.first is obj. Really the javascript engine isn't creating infinite objects, just an object that has a reference to itself.
It doesn't throw an error simply because the value of a property of an object is allowed to be a reference to that object. You don't have any actual recursion here.
You'd only get an error if, for example, you wrote a function to recursively crawl down through the properties of the object, but that would be because you had too many functions on the stack and the self reference would only be a factor that led to that.
There's no recursion here. What you have is a self reference (where one object stores a property that refers to itself).
Your code does not throw because console.log() is smart enough to handle self references. It doesn't infinite loop just because of a self reference as it is coded to specifically handle that situation.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to figure out how to access a variable inside a .when.done function. here's the example:
var colviews = {
1: true,
2: true,
3: false
}
$.when( // run only -when- these script get loaded
$.getScript( "/mckinney_images/jquery.tablesorter.all.js" )).done(function(){
$(function(){
console.log(colviews); // How do I get the colviews variable here?
});
});
I have a basic understanding of scope but not sure how it applies inside a when function.
I have a basic understanding of scope
Good! So you know what scope is, how closures work and how var works.
…but not sure how it applies inside a when function.
Simple: It's no different! Yes, the done callback function is invoked asynchronously, but that doesn't change how scope works.
You just are getting the colviews variable there successfully (if it doesn't log the expected value, that means it was overwritten elsewhere or the callback is never called because $.getScript failed).
$.when does no magic (and in fact, in your example it's not needed at all, your code works the same when you omit it).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can someone tell me what is happening here?I only know that it is requiring some core modules.
var inherits = require('util').inherits;
var express = require('express')();
The util module has exported an object that contains (probably amongst others) a function under the key inherits:
exports = {
inherits: function() ...
}
The express module on the other hand, has directly exported a whole function, and that function is immediately invoked and the result assigned to the variable express.
module.exports = exports = function() {
return ...
}
It is likely that the function has also returned an object containing key/value pairs of functions, just like you'd get from a normal exports object.
See also What is the purpose of Node.js module.exports and how do you use it?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've the following code:
function hi(){
alert("hi");
}
window["hi"]();
I thought the hi will be executed. Is there anything similar, that I could do? (no eval please)
Edit:
I want it in bracket notation as the function names will be in string.
If it doesn't work, you're probably in the wrong scope, try
window.hi = function(){
alert("hi");
}
window["hi"]();
EDIT:
If testing in jsFiddle, the default option of onLoad, or the onDomReady option inserts an event handler that waits for DOM ready with a callback.
The callback function creates a new scope, so when adding properties to the window object you have to reference it specifically with window.prop, just typing prop is not enough as it's not in the right scope.
You can just call it directly:
hi();
Or
window.hi();
if you really want to involve the global object.
edit if you want to access the function by a dynamic name, then you certainly can, but the function must be accessible as a property of some object. The global object is one possibility:
function hi() {
alert("hi");
}
window.hi = hi;
Then window["hi"]() will work. Alternatively, you can create an object for the purpose:
var functionRegistry = {}
function hi() {
alert("hi");
}
functionRegistry.hi = hi;
Then functionRegistry["hi"]() will work.