-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.

  • `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? – Dai Jun 03 '14 at 06:18
  • 1
    `afunction` doesn't have the property of `randomfunction `, so `afunction.randomfunction ` is `undefined`. – xdazz Jun 03 '14 at 06:18
  • @Dai i just want to get value of name ie somename by using afunction.randomfunction(function() { }) – user3700265 Jun 03 '14 at 06:19
  • @xdazz can you provide me an example in answer to add property to randomfunction .so that i can understand it better – user3700265 Jun 03 '14 at 06: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. – jfriend00 Jun 03 '14 at 06:25

2 Answers2

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);
Dropout
  • 13,653
  • 10
  • 56
  • 109
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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.

Community
  • 1
  • 1
Dropout
  • 13,653
  • 10
  • 56
  • 109
  • 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 ) – user3700265 Jun 03 '14 at 06:36