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

Zery
  • 3
  • 2
  • 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 '15 at 04:25
  • 2
    Aren't you getting syntax error ? – Vigneswaran Marimuthu Apr 24 '15 at 04:26
  • I would expect z:myFunc().method1() – mplungjan Apr 24 '15 at 04:31
  • @mplungjan—I'd expect *undefined* (if the syntax error is fixed), since *myFunc* doesn't have a return statement. – RobG Apr 24 '15 at 04:32
  • Method1 does hjowever have a return value – mplungjan Apr 24 '15 at 04:33
  • *Method1* is a label followed by a function declaration without a name (hence the syntax error). Giving the function a name fixes that, but that function isn't called, so *myFunc* returns *undefined*. – RobG Apr 24 '15 at 04:35
  • edit the question and sorry changing method1 to omaps. – Zery Apr 24 '15 at 06:11
  • Even though there's an answer, you might still find it hard to understand. No offense, but you need to learn fundamentals first. – Leo Apr 24 '15 at 06:40
  • @Leo actually I did, maybe did'nt good enough, anyway I use the above code by modifying from here http://stackoverflow.com/a/4892252/163812. So in my understanding it should not get any wrong. – Zery Apr 24 '15 at 07:30

2 Answers2

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
RobG
  • 142,382
  • 31
  • 172
  • 209
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);
Yogesh Khatri
  • 1,180
  • 8
  • 11
  • 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 '15 at 06:45