jQuery - Function not defined [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have two .js files. On the first .js file i declare and execute a function like this:
(function($){
function something(){
//code here
}
$(document).ready(function(){
something();
});
})(jQuery);
Also, I want to execute the something() function in the second .js. My code is the following:
(function($){
$(document).ready(function(){
//more code
something();
//more code
});
})(jQuery);
However, when the program is executed, I get the following error:
Uncaught ReferenceError: something is not defined(…)
Possible Solution:
I solved the problem declaring the function as:
(function($){
this.something = function(){
//code here
}
})(jQuery);
Is this 100% right?

You cant define a function inside a scope and try to use that function in another scope. You have not understood how functions and scopes work in JavaScript.
Have a look at this.

It's normal, you declared your function something in an IIFE, so you can't using it outside your block. Try to declare your function outside (function($) ...)() block.

Related

Jquery: scope in a .when .done function [closed]

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).

Why calling a function through bracket notation [closed]

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.

How can I pass a function as parameter in javascript? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to create a function with a callback function, but the solutions I've found does not allow to set the function.
I'll explain:
this is the solution I've found:
function callbackFunction()
{
alert("hello world");
}
function myFunction(callback)
{
callback()
}
myFunction(callbackFunction()) /* this works */
this is what i need:
function myFunction(callback)
{
callback()
}
myFunction(function(){alert("hello world");}); /* this doesn't work */
Some ideas?
Thank you
myFunction(callbackFunction()) /* this works */
No it doesn't. At least not in the way you think it does. This is:
Invoking callbackFunction
Passing the result of callbackFunction to myFunction
You're probably getting an error from within myFunction when it tries to invoke callback since that's not a function. But you're ignoring that because you see the alert() and think it works. The alert() happened before myFunction was invoked.
You want to pass it as a function reference, not a function invocation:
myFunction(callbackFunction) /* this works */
This would produce the same visible result (the alert()) but in the expected order of operations and without the error.
myFunction(function(){alert("hello world");}); /* this doesn't work */
You sure about that? If that is indeed "not working" for you then there must be more to the problem that you're not sharing with us, because that code works as-is.

Pass function as parameter and bind it to click [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have two files:
File A:
var A = (function () {
return {
bindClick: function (fun) {
$('#btnId').on('click', fun);
}
};
}());
File B:
(function ($) {
$(document).ready(function () {
var doSomething = function (what) {
console.log(what);
}
A.bindClick(doSomething(1));
});
})(jQuery);
This print me "binded" only once and when I click button nothing happen..
What is the right way to bind a function passing as parameter?
Problem is that I called:
A.bindClick(doSomething(1));
But I was wrong the behavior of the function: it had to return a function, in this way:
var doSomething = function(what) {
return function() {
console.log(what);
}
}
I hope it will be useful to someone!
Besides being somewhat convoluted way of doing things, it seems like it should work. And indeed it does work in the jsfiddles that the commenters have linked to...
I'd guess there is some other code not in your example causing problems. Are there any error messages in the javascript console?
EDIT:
The updated code where A.bindClick(doSomething); is changed to A.bindClick(doSomething(1));, does not work since you're firing the doSomething method immediately and bindClick receives the return value undefined from that function call.

Is there any catch/gotcha in this JavaScript boilerplate? [closed]

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.

Categories

Resources