This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 9 years ago.
I was told that you can declare functions in JavaScript more than 1 way.
ex.
// One way
function sqrt(x){
return x * x;
}
// Second way
var sqrtAlt = function (x){
return x * x;
}
What is the difference between these two function declarations?
The output is same but must have a reason to have two ways?
I am also curious about how you would use them.
Lastly, are there any other ways?
Thanks.
When you are defining
function sqrt(x){
return x * x;
}
is that the function name appears in Firebug debugger.
Functions that are declared as
var sqrtAlt = function (x){
return x * x;
}
come up as anonymous.
Also check out this Thread
They are basically the same thing, but in the second example you additionally assign the function to a variable. This way of creating a function is very useful when overriding an existing function of some object, let's say:
window.alert = function(text)
{
// Do something ...
};
Related
This question already has answers here:
Referencing "this" inside setInterval/setTimeout within object prototype methods [duplicate]
(2 answers)
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
Closed 5 years ago.
This question is from an written test for a company. It looks very confusing. I thought it will print whatever this.name is set to. Bu when I typed the code it shows nothing. I have little knowledge about closures and I think it is related to the problem. I want a little explanation here.
function dd(name) {
this.name = name;
this.go = function() {
setInterval(function() {
return this.name;
}, 2000)
}
}
var tt = new dd("corolla");
tt.go()
You can't get the return value from setInterval in this way. Try with a callback as in the following snippet
function dd(name)
{
this.name=name;
console.log( name );
var _this = this;
this.go=function(cb)
{
setInterval(function() {
cb(_this.name);
},1000)
}
}
var tt=new dd("corolla");
tt.go(function(ret) {
console.log( ret );
})
Also, please note that inside setInteval the value of this is not the same as in the otter function. That's why var _this=this;
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
I have searched the internet trying to find a good answer for this question. What I mainly found is suggestions to move the variable in the global scope, or using the functions as parameters for the function I want to use the variable in, but without explanation on how that works
To explain my dilema, lets say we have this piece of code:
function foo(){
var x = 2;
}
function bar(){
var z = 2;
}
function compare(foo,bar){
if ( z === x ) {
console.log("text");
}
}
This is the problem I'm facing. Is the code I've written above correct and if I call the compare() function it should console log "text" ?
declare with globel variable it's will easy to pass Another function
var x;
var y;
function foo(){
x = 2;
}
function bar(){
z = 2;
}
function compare(){
if ( z === x ) {
console.log("text");
}
}
foo()
bar()
compare();
This question already has answers here:
Constructors in JavaScript objects
(19 answers)
Closed 7 years ago.
I have JavaScript function as object:
function hexMesh(){
var side=25;
console.log('hexMesh');
function drawOver(){
}
}
As you can see it has a function call drawOver.
I try to call it by using a constructor as follows:
window.onload = function() {
hexMeshObj=new hexMesh();
hexMeshObj.drawOver();
}
But it gives me error saying undefined is not a function
Now I know I could declare the function in the prototype of the object but I don't want to do that.
Here it is on JSFiddle.
You can't use JavaScript like that!
Solution: Use Class-like prototypes (please don't treat them as classes, although they provide inheritance)
var x = function(v){ this.test = v; } // This is your constructor
x.prototype.show = function(){ console.log("TEST", this.test); } // This is a prototype with this as a context
var y = new x(true);
y.show(); // Logs TEST true
Edit:
Alternatively (although the prototype way is better as it provides real inheritance the oop way)
var x = function(v){
var context = this;
this.test = v;
this.show = function(){
console.log('test', context.test)
});
Another alternative way is to use bind to bind context if you need it.
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Is there a way to wrap all JavaScript methods with a function?
(3 answers)
Closed 8 years ago.
Here's what I want to achieve:
var obj = {
something: 1,
someFunction: function () {
return 'hello';
}
};
wrap(obj).someFunction('hello'); // this should be different from obj.someFunction defined above
wrap(obj).something = 2; // this should still work
Basically, I want to 'wrap' an object and replace all the functions in it with something else.
Here's how I've defined the wrap function:
function hello (v) {
return 'Hello ' + v;
}
function wrap (oldObj) {
var newObj = {};
for (var k in oldObj)
Object.defineProperty(newObj, k, {
get: function () {
if (typeof oldObj[k] === 'function')
return hello;
return oldObj[k];
},
set: function (val) {
oldObj[k] = val;
}
});
return newObj;
}
(hello is just an example replacement function)
When I try to use this, everything in obj gets replaced with a function (i.e even obj.something, which was set to 1, is replaced with a function). I can't seem to find any issues with this code. I've tried debugging it and haven't found the issue yet.
What might be the issue? How else can I solve this problem?
Edit 1: I don't want to replace functions in the object itself, I want to create an entirely new object.
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 9 years ago.
I see named functions exampled this way:
var clearClick = function() {
// do something
}
...and then used for binding like so:
$("#clear").bind(click, clearClick);
...or with the "shorthand" methodology thus:
$("#clear").click(clearClick);
But why not use a more "normal" (similar to other programming languages) construct like this:
function clearClick() {
// do something
}
It works the same, doesn't it? Is there any disadvantage to defining functions in this traditional way? Is the previous way just jQuerians flaunting their newfangledness?
This works Function expression
var clearClick = function() {
// do something
}
$("#clear").bind(click, clearClick);
This does not work Function expression. The order matters here.
$("#clear").bind(click, clearClick);
var clearClick = function() {
// do something
}
But when you declare your function using a function declaration the order does not matter.
One more advantage of the below syntax is that the function name appears in debugger.
function clearClick() {
// do something
}
One reason you might want to do it is how this works:
var clearClick;
$("#clear").click(clearClick);
clearClick = function() {
// do something
}
... lots of stuff in here ...
clearClick = function() {
// do something different
}