Use a method in a callback [closed] - javascript

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 9 years ago.
Improve this question
Question is being edited. Sorry for changes. Please don't provide answers at the moment :)
If I declare an object in javascript:
var MyObject = {
function myFunction() {
console.log('hi!');
}
}
Why do I get errors?

Wouldn't it rather be:
var MyObject = {
myFunction : function(){
console.log('hi!');
}
}
How to call the method:
MyObject.myFunction()

Related

How can I make a variable static in Js? [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 2 years ago.
Improve this question
I want the newid to be incremented when I call the function.
You make the function close over the variable, by declaring the variable in the surrounding context:
let newid = 3;
const generateTemplate = contact => {
// ...
newid++;
};

Explain the shortest JS function creation using a key of an object [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 5 years ago.
Improve this question
var o = {["k"](){}}; // {k: ƒ}
o.k();
Here you can see an example:
How {["k"](){}} is compiled to an object which has a key k inside which is a function?
{["k"](){}}
equals:
{
k(){}
}
Which is ES2015 short method syntax for:
{
k: function(){}
}

convert jquery to javascript? [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 5 years ago.
Improve this question
I want to convert this jquery to javascript.
Please help
$(document).ready(function($){
if ( $("#search_field").val().length > 0 )
{
$('*[data-role=activerecord_sortable]').activerecord_sortable();
}
});
Here is how it could look like using addEventListener and querySelector
Note, the activerecord_sortable() is Ruby
window.addEventListener('load', function() {
if (document.querySelector('#search_field').value.length > 0 )
{
document.querySelector('*[data-role=activerecord_sortable]').activerecord_sortable();
}
})
Here's what I can come up with, I don't know what your activerecord_sortable function is doing off the top of my head:
document.addEventListener('DOMContentLoaded', function(){
var searchField = document.getElementById('search_field');
if(searchField.value) {
searchField.dataset['data-role'] = activerecord_sortable();
}
});

How can i get array key-values pair using javascript [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 7 years ago.
Improve this question
for(i=0;i<=array_values.length;i++){
var a=array_values[i].amounts;
var t=array_values[i].Tax;
alert(a);
alert(t);
}
Error : TypeError: array_values[i] is undefined
var a=array_values[i].amounts;
var a=array_values[i].amounts;
change
for(i=0;i<=array_values.length;i++)
to
for(i=0;i<array_values.length;i++)
<= to <

How do I catch the response from a named function? [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 9 years ago.
Improve this question
I'm calling a function like this:
$('.qa-nav-main-ask a').click(showAskForm);
Show showAskForm is this (simplified):
function showAskForm(){
return true;
}
I want to catch the response from showAskForm and save it to a variable, I tried
askFormShowing = $('.qa-nav-main-ask a').click(showAskForm);
without success.
function showAskForm(){
return true;
}
var askFormShowing;
$('.qa-nav-main-ask a').click(function(){askFormShowing = showAskForm();});
Try the above
return is a reserved word and cannot be used as a JavaScript identifier. So, use like this:
$('.qa-nav-main-ask a').click(showAskForm);
function showAskForm(){
myvar = true;
}

Categories

Resources