-2
        function myFunc() {
        omaps: function tes() {
            return this.parent.x + this.parent.y;
        }
    }

    var myobj = {
        x: 1,
        y: 2,
        z: myFunc()
    }

    window.alert(myobj.z);

return undefined

        function myFunc() {
        omaps: function tes() {
            return myobj.x + myobj.y;
        }
    }

    var myobj = {
        x: 1,
        y: 2,
        z: myFunc()
    }

    window.alert(myobj.z);

return undefined

        var myobj = {
        x: 1,
        y: 2,
        z: {
            omaps: function() {
                return myobj.x + myobj.y;
            }
        }
    }

    window.alert(myobj.z);

return [object, Object]

From the snippets above I get a return with undefined or [object, Object] instead of value 3.

How do I return the value I expected? Do I do it right or wrong?

9
  • 3
    That code is not syntactically correct, you should not be able to get (Object, object). Please check that you have pasted it correctly first. Also - note that method1: here is not a method - if anything, it would be a label (if the rest was syntactically correct).
    – Amadan
    Apr 24, 2015 at 4:25
  • 2
    Aren't you getting syntax error ? Apr 24, 2015 at 4:26
  • I would expect z:myFunc().method1()
    – mplungjan
    Apr 24, 2015 at 4:31
  • @mplungjan—I'd expect undefined (if the syntax error is fixed), since myFunc doesn't have a return statement.
    – RobG
    Apr 24, 2015 at 4:32
  • Method1 does hjowever have a return value
    – mplungjan
    Apr 24, 2015 at 4:33

2 Answers 2

0

In the first two examples you get undefined because the value assigned to myobj.z is the result of calling myFunc.

That function doesn't have a return statement, so it returns undefined and that is the value assigned to myobj.z.

In the third example, you are assigning an object to myobj.z, so you see the result of calling the toString method inherited from Object constructor (i.e. the one on Object.prototype), which returns '[object Object]'.

Probably what you want to do is:

// Function assumes *this* will be set in the call
function myFunc() {
    return this.x + this.y;
}

var myobj = {
    x: 1,
    y: 2,
    z: myFunc  // note assignment, no ()
}

window.alert(myobj.z());  // Note *this* must be set in the call and z must be called

You could also use a getter (supported in modern browsers):

var myobj = {
    x: 1,
    y: 2,
    get z(){
      return this.x + this.y;
    }
}

window.alert(myobj.z)  // 3
0

you can assign the function to myobj.z and use this.x and this.y to get the properties and getting the value by calling myobj.z() like this

function myFunc() {
        return this.x + this.y;
    }

    var myobj = {
        x: 1,
        y: 2,
        z: myFunc
    }

    window.alert(myobj.z());

another way is to pass the the object in the function to get the properties and assign return value to myobj.z like this

function myFunc(obj) {
        return obj.x + obj.y;
    }

    var myobj = {
        x: 1,
        y: 2,
        z: myFunc(myobj)
    }

    window.alert(myobj.z);
1
  • Have you tried the second method? You might find you get a type error since myobj has a value of undefined when myFunc is called.
    – RobG
    Apr 24, 2015 at 6:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.