What is the difference between these two object methods in Javascript? [duplicate] - javascript

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Prototyped and a non-prototyped method? [duplicate]
(1 answer)
Closed 8 years ago.
Consider the following code:
function someObj() {
this.someFunction = function() {
console.log('someFunction');
}
}
someObj.prototype.foo = function() {
console.log('foo');
}
I can call both like so:
var test = new someObj();
test.someFunction(); // Logs 'someFunction'
test.foo(); // Logs 'foo'
How would variable scope be affected here? Is there an advantage / disadvantage to each approach?

Related

Multiple uses of a function in a JavaScript Object [duplicate]

This question already has answers here:
Losing "this" context in JavaScript when passing around members [duplicate]
(3 answers)
How to access the correct `this` inside a callback
(13 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed last year.
If I use the first representation, the console will log the name, however in the second one it does output "JS Bin Output", any idea what happened?
let person = {
name: 'Fred',
sayMyName: function(params) {
return (this.name);
},
};
let obj = person;
person = null;
let sayMyName = obj.sayMyName;
console.log(obj.sayMyName()); // logs `Fred`
console.log(sayMyName()); // logs `JS Bin Output`

Difference between `function` and `() =>` with respect to `.call()`? [duplicate]

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Using arrow functions with .call()? "This" varies based on if in devtools or iojs locally? Should one avoid using arrows with .call?
(1 answer)
Can you bind 'this' in an arrow function?
(14 answers)
Closed 5 years ago.
I know about using .call() and .apply() to set the this for a function call, but can I use it for a lambda?
Consider this example from the MDN webdoc for Function.prototype.call:
function greet1() {
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
}
let greet2 = () => {
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
}
var i = {
person: 'Douglas Crockford', role: 'Javascript Developer'
};
greet1.call(i); // Douglas Crockford Is An Awesome Javascript Developer
greet2.call(i); // Is An Awesome Javascript Developer
Why doesn't the greet2 call work the same was as greet1 and is there a way to set this in the case of greet2?

Javascript object closure context [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 6 years ago.
I'm trying to write some JS object. Getting stuck on something, I don't understand how contexts work. Here is a simple example :
var MyApp = function(el) {
this.el = el;
};
MyApp.prototype.bind = function() {
window.setTimeout(this.start, 300);
}
MyApp.prototype.test = function(msg) {
console.log(msg);
}
MyApp.prototype.start = function() {
console.log(this); // Returns the window context
this.test('Hello'); // Doesn't work, of course.
}
var myapp = new MyApp(el);
myapp.bind();
Problem is when calling the start method, I'm in the window context because of the window.setTimeout. Is there a way to fix this or is it a pattern design issue?
Thanks ;)

Is there a way to declare and instantly run function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 6 years ago.
I've a code:
var myFn = function(){
//some code
}
myFn();
So I have to define a function, then run it in two rows.
Is there a way to define a function (with storage it in variable) and run it instantly, in one expression? Just a short way of this.
var myFn = function(){
console.log("test");
}()
myFn will be undefined because the function doesn't return anything

JavaScript - Difference beetween object's method named once and name twice [duplicate]

This question already has answers here:
declare function name in object, Why? [duplicate]
(3 answers)
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
First of all, sorry for bad title or description, I'm not completely good with English.
I want to know what is difference between this line of code:
var obj = {
...
func: function func() { ... },
...
}
and this:
var obj = {
...
func: function() { ... },
...
}
What is it special in naming a method twice? I saw both of these ways in a single JavaScript source code. Here you can take a look at source if it's needed.
Edit: Question is not about anonymous or non-anonymous function declaring, but about functions inside objects that are called methods.
One of the biggest (and most helpful) differences is that the non-anonymous function will provide the function name in stack traces.
The named version can be used recursively as Teemu points out.

Categories