Forums

  • # October 14, 2013 at 2:11 pm

    okay = new Object{

    function=testing(){
    return 5+6;
    }

    }
    document.write(okay.testing());

    # October 14, 2013 at 3:47 pm
    var obj = {
        a: 5,
        b: 6,
        sum: function (a, b) {
            return this.a + this.b;
        }
    };
    
    alert(obj.sum()); // 11
    
    # October 14, 2013 at 3:59 pm

    You can also make this little bit modular:

    var CreateObject = function (a, b) {
        this.a = a;
        this.b = b;
    
        this.sum = function (a, b) {
            return this.a + this.b;
        };
        this.dif = function (a, b) {
            return this.a - this.b;
        };
    };
    
    var result1 = new CreateObject(5, 6);
    var result2 = new CreateObject(10, 34);
    var result3 = new CreateObject(34, 3);
    var result4 = new CreateObject(45, 300);
    //alert(result1.sum()); // 11
    //alert(result1.dif()); // -1
    //alert(result2.sum()); // 44
    //alert(result2.dif()); // -24
    //alert(result3.sum()); // 34
    //alert(result3.dif()); // 31
    //alert(result4.sum()); // 345
    //alert(result4.dif()); // -255
    
    # October 14, 2013 at 4:00 pm

    http://jsfiddle.net/fWwbe/

    # October 15, 2013 at 11:55 am

    @CrocoDillon That’s a great point, since the sum and dif get created each time, for each object… could just define them on the prototype… I didn’t want to go into memory management just yet.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag