Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I encountered a problem that I can not seem to understand, why if define tick function like this;
function tick () { /* do something */ }
It works fine and in this mode
var tick = function () { /* do something */ }
doesn't work. Where is the problem?
working example of first http://jsbin.com/omokap/10/edit
working example of second http://jsbin.com/isobul/1/edit
The problem is that you are using tick before its definition in one case and after its definition in the other case.
In the first case:
force.on("tick", tick);
function tick () { /* ... */ }
the function tick is defined at parse time and is available to be passed as the second argument.
On the other hand in:
force.on("tick", tick);
var tick = function () { /* ... */ };
the variable tick is defined at parse time (so JSHint does not complain), but it gets its value only at runtime. Its value is undefined when force.on("tick", tick) is executed.
The difference is more apparent when you consider the following example:
var f;
if (true) {
f = function () { return 1; };
} else {
f = function () { return 2; };
}
f(); // returns 1
versus:
if (true) {
function f () { return 1; }
} else {
function f () { return 2; }
}
f(); // returns 2, from the latest definition
See this question to understand the difference between using the var tick = function () ... and function tick() ... better.
Related
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 1 year ago.
Improve this question
I'm trying to get these functions defined to run some synchronous and asynchronous test cases on them with Mocha and Chai in JS, what am I doing wrong? Why is my editor marking certain lines?
module.exports = {
function myFunctiona () {
}
function myFunctionb () {
for (let i = 0; i < 10000; i++) {
new Date();
}
}
function myFunctionc(done) {
setTimeout(done, 0);
}
function myFunctiond (done) {
setTimeout(done, Math.round(Math.random() * 10));
}
}
This is a syntax error because you're defining an object with properties, but you don't have property keys. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_colon_after_property_id for more information.
Typically, you'd define an object like the following
(note the commas after each property too):
var object = {
property1: 'thing',
property2: function() {
return 'thing2';
}
}
so to change your functions to properties, set the property key as the function name, and then assign a function to it like:
module.exports = {
myFunctiona: function () {
//nothing
},
myFunctionb: function () {
for (let i = 0; i < 10000; i++) {
new Date();
}
},
myFunctionc: function (done) {
setTimeout(done, 0);
},
myFunctiond: function (done) {
setTimeout(done, Math.round(Math.random() * 10));
}
};
This question already has answers here:
Different ways to execute IIFE?
(2 answers)
Closed 8 years ago.
I've been using the following to begin filling out code for an IIFE:
(function() {
/* code goes here */
}());
On occasions I see the following being used:
(function() {
/* code goes here */
})();
Which is the correct one?
According to Douglas Crockford (the creator of jslint) the first one is less error prone when another developer reads your code. But not everyone has to respect this, both are fine though it is good to know what exists, and why.
When a function is to be invoked immediately, the entire invocation
expression should be wrapped in parens so that it is clear that the
value being produced is the result of the function and not the
function itself.
var collection = (function () {
var keys = [], values = [];
return {
get: function (key) {
var at = keys.indexOf(key);
if (at >= 0) {
return values[at];
}
},
set: function (key, value) {
var at = keys.indexOf(key);
if (at < 0) {
at = keys.length;
}
keys[at] = key;
values[at] = value;
},
remove: function (key) {
var at = keys.indexOf(key);
if (at >= 0) {
keys.splice(at, 1);
values.splice(at, 1);
}
}
}; }());
Its purely an aesthetic preference. Use whatcha like. Douglas Crockford tried real hard to popularize the first, but I more often see the second.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Module = (function () {
var Method = function () {
this.doSomething =function () {
return "doSomething";
};
}
return {
Method:Method
};
})();
var value=Module.Method.doSomething;
console.log(value);
New to Javascript, coming from a java background.
Why is value coming back as undefined?
Cheers.
You have to have an object of Method to access the property doSomething, you can achieve it like
Module = (function () {
var Method = function () {
this.doSomething =function () {
return "doSomething";
};
}
return {
Method:Method
};
})();
var value= new Module.Method();
alert(value.doSomething());
you can also approach it like
Module = (function () {
var Method = function () {
this.doSomething =function () {
return "doSomething";
};
}
return {
Method:new Method
};
})();
var value=Module.Method.doSomething();
alert(value);
Use as
var value= new Module.Method().doSomething();
DEMO
Module.Method is a function, apparently designed to be used as a constructor. You're also not calling the function.
var value=new Module.Method().doSomething();
^^^ create an instance of Module.Method
^^^ call the doSomething method of 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.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have some functions, like:
function functionOne() {
/*
code
*/
}
function functionTwo() {
/*
code
*/
}
function functionThree() {
/*
code
*/
}
How can I write code to call each of the functions, once each (i.e. without repeats), in a random order?
Put your functions in an array, randomise the array and then iterate through it:
var functions = [
function () { alert("function 0"); },
function () { alert("function 1"); },
function () { alert("function 2"); },
function () { alert("function 3"); },
function () { alert("function 4"); }
];
functions.sort(function() { return 0.5 - Math.random() });
for (var i=0;i<5;i++) {
console.log(i);
functions[i]();
}
Here's a fiddle
this function should work:
function randCall(){
var array = [1,2,3];
while(array.length > 0){
var rand = (Math.random*array.length)|0; //converts to an Integer value
switch(array[rand]){
case 1:
functionOne();
break;
case 2:
functionTwo();
break;
case 3:
functionThree();
break;
}
array.splice(rand, 1);
}
}
hope this works and helps.
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
// set Process
i18n.setProcess(function() {diFunctions.getI18n('http://localhost/service/i18n/page?lang=eng&group=staff') });
// Setter and getter
this.setProcess = function( opProcess ) { //here param opProcess is function with parameters see i18n.setProcess() line of code
if( opProcess == undefined)
throw "Process is undefined";
if( $.isFunction(opProcess) == false )
throw "Process is not a function"
process = opProcess;
};
this.getProcess = function() {
return process;
};
See how i18n.setProcess passes a function with param as a parameter to setProcess.
Now i what i want in SetProcess is function() {diFunctions.getI18n('http://localhost/service/i18n/page?lang=eng&group=staff',**id**) // id is added dynamically to the function itself which was passed as parameter to setProcess
Problem - I want to add id dynamically(defined in my class variable always accesible by id) on set process with addition to functions parameter(Url,etc,etc,id). Functions parameters can grow but the id should be added last as a parameter?
Tried quite a few solutions but didnot work out? Check here
This is what the arguments object is for..
function foo() {
var lastArg = arguments.length ? arguments[arguments.length - 1] : 'default';
return lastArg;
}
foo(); // "default"
foo(1); // 1
foo(1, 2); // 2
If you want to write a function similar to bind which only sticks arguments on the end then you could do
function appendArguments(fn) {
var slice = Array.prototype.slice.call.bind(Array.prototype.slice),
args = slice(arguments, 1);
return function () {
return fn.apply(this, slice(arguments).concat(args));
};
}
now
var bar = appendArguments(foo, 3);
bar(); // 3
bar(4, 5); // 3 (because it's calling `foo(4, 5, 3)`