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.
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 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.
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 6 years ago.
Improve this question
I am getting the error:
bluebird.js:2118 Uncaught TypeError: generatorFunction must be a function
But I don't understand why this error is occurring, as I am passing it a generatorFunction.
export class Welcome {
fetch() {
Promise.coroutine(this.userData());
}
userData = function* getData() {
this.lotsData = yield this.testApi.getMock();
this.lotsData = JSON.stringify(this.lotsData, null, 4);
}
}
So a click event calls fetch() and that calls this.userData(). This is the console dump of this.userData:
GeneratorFunctionPrototype {}
_invoke: invoke(method, arg)
__proto__: GeneratorFunctionPrototype
Which tells me it most certainly is a generator. I am using all of this in an aurelia class if that somehow makes any difference (which it shouldn't I don't think). So why is it erroring that the generatorFuction must be a function?
The limited amount of code you posted is riddled with errors, but there is not enough to know what is correct and what is incorrect.
One thing is for sure this.userData() is a function call and not a reference to a function, which is what .coroutine() is expecting.
Whatever type returned by this.testApi.getMock() is what is being yielded, which we have no idea given what you posted, but that is probably not even relevant at this point, because what is returned is probably unknown/null anyway because you are assigning the yield to a variable of questionable scope. Your code is nowhere near the example from the documentation
PingPong.prototype.ping = Promise.coroutine(function* (val) {
console.log("Ping?", val)
yield Promise.delay(500)
this.pong(val+1)
});
The documentation has a very clear and well defined example you should follow, explicitly until you understand what you are actually doing.
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
NOTE: This entire question was based off a scenario where the issue was a syntax error; there was actually NO issue with passing 'this'. The question should be closed.
For example, the following works:
$('.base-icons').click(function () {
selectedIcon($(this).attr("src").split(/\/(\/*)/));
});
var selectedIcon = function(myObj) {
console.log(myObj);
};
And prints the shortened string as expected. The follow does not work:
$('.base-icons').click(function () {
selectedIcon(this);
});
var selectedIcon = function(myObj) {
console.log($(myObj).attr("src").split(/\/(\/*)/)[6]);
};
as it prints undefined. Why? Thank you.
For example, the following works:
selectedIcon($(this).attr("src").split(/\/(\/!*)/)[6])
var selectedIcon = function(myObj) {
console.log(myObj)
}
And prints the shortened string as expected. The follow does not work:
selectedIcon(this)
var selectedIcon = function(myObj) {
console.log($(myObj).attr("src").split(/\/(\/!*)/)[6])
}
as it prints undefined. Why? Thank you.
selectedIcon is undefined when called at javascript at Question, resulting in a TypeError
Both examples at Question should return an error.
Uncaught TypeError: selectedIcon is not a function
window.onerror = function(e) {
console.info(e);
alert(e);
}
selectedIcon("abc");
var selectedIcon = function (myObj) {
alert(myObj)
}
Yes, you can access attributes of this when passing this through a function.
You can access the attributes of this like this.
$(this).attr("href");
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.