Javascript calling other object method - javascript

I have these two objects: A and B. I want to call A.something from B, but is not working...
A = function()
{
function something()
{
//do something
}
}
B = function()
{
A.something();
}
this throws "typeError, A.something(); is not a function"...

Your current code attempts to use A as an object when it is a function. You would need to invoke the function A(), but then its something method would still not be available (because it is not exposed).
If you want A to be an object, you could use an object literal like this:
A = {
something: function()
{
//do something
}
}
B = function()
{
A.something();
}
Or for a more classical looking approach you could use new:
function A()
{
this.something()
{
//do something
}
}
B = function()
{
var a = new A();
a.something();
}
There are more ways as well. You can use Object.create or use a more functional approach by returning an object inside the A function.

Don't declare A as a function itself, declare it as an object:
A = {}
Then inside place something as your function:
A = {
something: function() {
//do something
}
}
You will now be able to call A.something().

First of all, to create an object in javascript you have to define your class A like this (easy approach, not using prototyping):
A = function()
{
return {
something = function()
{
//do something
}
}
}
Then create your object by calling var object = A().
So your code should look like this in the end:
A = function()
{
return {
something = function()
{
//do something
}
}
}
B = function()
{
var aObject = A();
aObject.something();
}

Related

What's the difference between these two statements in Javascript?

Is there any difference between these two snippets apart from just pure preference in which you use? As I've seen both used a lot and am not sure how/why they differ other than just preference in terms of readability.
object.foo = function () {
//code
}
foo : function () {
//code
}
This:
object = {
foo : function () {
// code
}
}
is the complete way to write the second statement, meaning you define the function in the initialization of the object.
This code:
object.foo = function () {
//code
}
can be used both as a shorthand, and if you want to declare the function dynamically after the declaration of the object.
They do exactly the same thing in the example you've given, but there are some slight differences which explain why there are seemingly 2 ways to do the same thing.
Take this example...
var obj = {
foo: function() {
// do something
}
};
You can then extend that object like this...
obj.bar = function() {
// do something different
};
But if you did this...
obj = {
bar: function() {
// do something different
}
};
... you would lose the function foo. So one method is for explicitly declaring an object, complete with properties and functions, and the other method can be used for extending an existing object.
Just to be perfectly clear, this is incorrect syntax...
var obj = {
foo = function() { // it should be foo: function() {}
// do something
}
};

How to call recursively a function stored in a variable in Javascript

How can I achieve that in Javascript?
exports.functions = {
my_func: function (a) {
my_func(a);
}
}
A function expression can have an optional name after the function keyword. The scope of this name is just the function body, and allows it to be called recursively:
exports.function = {
my_func: function this_func(a) {
this_func(a);
}
}
You can also use its full name:
exports.function = {
my_func: function(a) {
exports.function.my_func(a);
}
}
This is not quite ok as a standard, but you can easily achieve this like:
var foo = function(a) {
foo(a);
}
exports.functions = {my_func: foo};
Your code does not work because in this case the hoisting is not done ok(well it's done ok) but not for what you need.

How can I run function automatically when calling object at each time?

How can I run function automatically when calling object at each time ?
I have the following object :
var myObject = {
main: function(e) {
//main
},
x: 1,
y: 2,
another: function(e) {
//another
}
}
Is it possible to achieve the below functionality?
Calling
myObject();
would call the main method.
But calling
myObject().another();
Would call the another method.
use only myObecjt() not myObecjt.main()
If I understood the question correctly, the code structure you're looking for is as follows:
var myObject = (function () {
var t = {};
t.main = function() {
console.log("main");
};
t.another = function () {
console.log("another");
};
t.main();
return t;
});
This will result in the following functionality:
Calling myObject(); will call the main method.
Calling myObject().another(); will call both the main and another methods.
if you are looking for jquery like chaining try something like this
function myObject(){
if(this == window){
return new myObject();
}
//whatever the main method does
return this;
}
myObject.prototype.x = 1;
myObject.prototype.y = 2;
myObject.prototype.another = function(){
//whatever the another method does
return this;
}
something like this, would recommend to investigate method chaining and prototypal inheritance, for a clean aplication of this.
or for something more simple
function myObject(){
//whatever the main method does
return myObject;
}
myObject.x = 1;
myObject.y = 2;
myObject.another = function(){
//whatever the another method does
return myObject;//method chaining may continue
}

Return an object from an object method in javascript

A = {
f1: function() {
return {
a: function(){ alert('sss'); }
}
}
}
A.f1().a();
Why is it used in this way?
Why is the method a() bound to A.f1()?
Member function f1 of A returns an object literal with its member a set to a function. It could've also been written as:
A = {
f1: {
a: function() { alert('sss'); }
}
}
A.f1.a();
Returning an object could in this case be personal preference. There is no functional difference between these two examples.
When you do as below:
var x = A.f1();
What you get on to x is the object returned by the f1 function. which is:
{
a: function(){ alert('sss'); }
}
now the object 'x' has function a() on it. You can call that function as:
x.a();
Which is exatly similar to:
A.f1().a();

How can i call AnotherSubFunction using my obj

var obj = {
MainFunction: function() {
AnotherSubFunction: function() {
}
var variable = AnotherSubFunction ()
}
}
Can i do something like this...
How can i call AnotherSubFunction using my obj? Is it possible.
How to create a function inside another function...
The code in your question is not valid Javascript. You're probably looking for:
MainFunction: function() {
function AnotherSubFunction() {
// ...
}
var variable = AnotherSubFunction();
}
Or maybe:
MainFunction: function() {
AnotherSubFunction = function() {
// ...
}
var variable = AnotherSubFunction();
}
However, in both cases, the name AnotherSubFunction associated with the nested function only exists in the scope of the enclosing function (MainFunction) and will not be accessible directly from obj.

Categories