Access method using object in javascript [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 8 years ago.
I have a functions A B with same method in them. I want to access this method from another function. Depends upon some condition ill choose any one of version!
function A() {
var method = function() {
// do something..
}
}
function B() {
var method = function() {
// do something..
}
}
function other() {
// access method from function A() or B();
}
is there any way to access using objects like we do in JAVA or any other way?

You could technically make "method" global by putting it on the window global object:
function A() {
window.method = function() {
// do something..
}
}
function other() {
// access window.method
}
Generally, this isn't what you want to do. If you find yourself having to do weird hacks like this to get access to a method it usually means something wrong. You should try to pull the method out into a shared scope where it's accessible where it's needed.

Related

Anonymous vs Named Functions in JavaScript Module Pattern? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 6 years ago.
Is there any benefit for the following JavaScript module defenition:
var module = (function(){
var PublicFnc = function(){ // variable declaration
alert('hi');
}
return {
f : PublicFnc
}
})();
module.f();
Over the following:
var module = (function(){
function PublicFnc(){ // function declaration
alert('hi');
}
return {
f : PublicFnc
}
})();
module.f();
Although the second example is more convenient since it is more similar to Java/C# the anonymous methods are used more often and I'm wondering what is the benefit?
#Alexander, thanks for marking the question as duplicate but I tend to leave it open since I'm asking for the benefits within the context of module patterns and not generally
One of the differences between them can be explained using a concept called hoisting
In case:
a(); //you cannot call this function before its definition as here var a is undefined
var a = function(){ //function statement
console.log("Hello World");
}
a(); //this will call the function even before its definition because of hoisting
function a(){ //function definition
console.log("Hello World");
}
Also some the function in the above case is assigned a memory space using that variable.
In hoisting, function definition is hoisted and not the function statement.
Read more about hoisting here http://www.w3schools.com/js/js_hoisting.asp
Also, when to use statement and when to use definition depends on use case and personal preference

'this' is undefined in the next scope [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 6 years ago.
My problem is as simple as the title.. I have some code which makes an AJAX call. This code is similar to this (JSFiddle):
function Test() {
this.name = "U don't wanna know my name..";
}
Test.prototype.ajax = function() {
$.ajax("url/path", data, function() {
alert(this.name);
});
};
var test = new Test();
test.ajax();
In this case this is undefined. I could place the following code before the ajax call and use that in stead of this:
var diz = this;
I was wondering if there's another way of using this without creating a new variable for it.
In this case this is undefined.
this.name is undefined (assuming you meant that), because this is specific to a function's context. Inside that ajax's callback handler this no more belonged to Test, it belonged to that callback function.
was wondering if there's another way of using this without creating a
new variable for it.
I don't think that without saving the reference to parent's this (Test's this) you can access this that belonged to a more global scope from a function's scope.

Javascript object creation practices [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
This is what I think is best for making an object in js
function obj(){
var x = "hi";
this.getX(){return x;}
}
var y = new obj()
console.log(y.x); //this returns undefined
But from what I have seen, using this.variable is used more often in object creation.
I am thinking in java where things should be "private" in a class (note I have read about closures), does that apply in js?
What is considered the best way of object creation?
It sounds like you're trying to apply Java concepts to JavaScript, but things works completely differently in JS. You should check out the MDN article on JavaScript closures.
var variables exist in the closure. They are accessible from any function declared in the same scope.
var me = 'hello';
function someFunction() {
console.log(me);
}
someFunction(); //prints 'hello' to console
this variables will be directly accessible, even outside the scope, in the resulting object.
function someFunction() {
this.me = 'hello';
}
var instance = new someFunction();
console.log(instance.me); //prints 'hello' to console

Get inside function calls [duplicate]

This question already has answers here:
How to generate call-graphs for given javascript? [closed]
(4 answers)
Closed 9 years ago.
I try to give a abroad picture from my question. Suppose I have a function, inside this function I have some other function calls and finally, first function returns a value.
function a() {
return 1;
}
function b() {
var result = a();
return 2 + result;
}
b();
Now, after calling b() method, I want to know how many and what functions called inside the b() function. How can I achieve it?
There is no orthodox method for doing this. You can do the opposite, however, find out who called the function a() by accessing it's arguments.callee.caller inside a().
But, if you are real serious about finding out what functions are called within your function, you can always do b.toString() and then parse the code manually :)
You need an AST generator. Use a javascript parser like this.

JavaScript myFunction : function(int x) vs function myFunction(int x)? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between a function expression vs declaration in Javascript?
What is the difference between the two ways to declare a function in JavaScript?
myFunction : function(variable)
{
}
or
function myFunction(variable)
{
}
Your first code snippet is not valid - it only works within an object; example:
var object = {
myFunction: function(variable) { }
};
// object.myFunction();
Basically there are two ways to define a function ins JavaScript:
function myFunction(variable) { }
var myFunction = function(variable) { };
The difference is: The first type of declaration uses the function statement and therefore allows you to use the function before it has been declared. Example:
console.log(myFunction()); // prints test
function myFunction(variable) { return "test"; }
Read more about it here.
This is not possible with the second type of function declaration, which assigns an anonymous function to a variable. The function can't be used before the variable has been declared.
One is a method. The other a function.
Functions are defined
function myfunction() {..}
Methods are defined
myobject.mymethod = function() {...} ;
A method is a property of an object that points to / is a function of that object
Really it depends on how you structure your objects. Functions are usually used in global libraries that are non object specific while methods are tied to objects to execute specific pieces of functionality.

Categories