-1

Hey guys am new to javascript development .i have learned about javascript objects and i have done some code with it ..My code

function afunction() { 
var num = 10;
return num;
}

afunction.randomfunction(function() {

return {

"name": "somename"

"age": 12
}

})

When i called the function with afunction.randomfunction.name; it gives me error like

" underfined is not a function"

i dont know why am getting like this .what i need is that i need to get the the value of name object by using afunction.randomfunction.name;

i know am doing something wrong ..Hope you guys can help me .to find out my mistake.

5
  • afunction.randomfunction.name is not correct code in this context because randomfunction doesn't exist, nor is it a child of afunction. Why do you have this code? What are you wanting to achieve? Commented Jun 3, 2014 at 6:18
  • 1
    afunction doesn't have the property of randomfunction , so afunction.randomfunction is undefined. Commented Jun 3, 2014 at 6:18
  • @Dai i just want to get value of name ie somename by using afunction.randomfunction(function() { }) Commented Jun 3, 2014 at 6:19
  • @xdazz can you provide me an example in answer to add property to randomfunction .so that i can understand it better Commented Jun 3, 2014 at 6:20
  • What is afunction.randomfunction supposed to be? Is it supposed to be a function? If so, you have to define it as a function before you can call it. Commented Jun 3, 2014 at 6:25

2 Answers 2

2

You are trying to call randomfunction and pass it a function expression instead of just assigning a function expression.

Change obj.foo(x) to obj.foo = x.

Then you can call it — obj.foo() — and access return values from it: obj.foo().property.


function afunction() {
    var num = 10;
    return num;
}

afunction.randomfunction = function () {
    return {
        "name": "somename",
        "age": 12
    };
};

alert(afunction.randomfunction().name);
Sign up to request clarification or add additional context in comments.

6 Comments

can you provide me an example ..i want to call two functions like function1.function2(function() { object_here } }) ..and return object value from here
Err… I did provide you with an example!
this was the one i wanted ..surely i would accept this answer
i have a doubt will it work if i use like afunction.randomfunction(function() { return { name: somename }; });
this dont work ..it says SyntaxError: Unexpected token )
|
1

This is happening because the function randomFunction isn't defined in aFunction. You need to either define it as a separate function like this

function randomFunction(){
    //code
}

and call it by randomFunction();

or if you want to create an object with a public function you can create it for example like this

function YourObject(){

    var num = 10; //think of this as a constructor

    this.getNum = getNum; //this "attaches" the getNum() function code to the getNum variable of YourObject
    function getNum(){
        return this.num;
    }

    this.randomFunction = randomfunction;
    function randomFunction(){
        //code
    }
}

afterwards you can call your object's method like this

var yourObject = new YourObject(); //instantiate
console.log(youObject.getNum()); //print the value of yourObject.num to console
yourObject.randomFunction(); //execute your random function

Note: There are more approaches on creating objects in JavaScript, this is just one. You might find Which way is best for creating an object in javascript? is "var" necessary before variable of object? interesting.

1 Comment

i dont want to do like this i want to do as @quentin said but when i run his code it shows SyntaxError: Unexpected token )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.