Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Why's this simple object not working?

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #152358
    javascriptnewbie
    Participant

    1: Do do you actually do with objects?
    Aren’t they the same thing as functions?

    2: Why’s this not working?
    http://jsfiddle.net/h123er2/26r3U/

    Thanks!

    #152360
    Kuzyo
    Participant

    we have two ways to create object in JS:

    var obj = new Object(); 
           obj.height = 10; 
           obj.width = 20;
    

    second, more often used:

    var obj2 = { 
           height: 10, 
           width: 20 
    }
    
    #152372
    javascriptnewbie
    Participant

    I just tried the second one and it doesn’t quite work as expected :(

    var object-name = {
    height:10,
    length:10
    }

    alert(height+length);

    #152379
    Alen
    Participant
    var myObj = {
      a: 10,
      b: 20
    }
    
    alert(myObj.a + myObj.b);
    

    Edited to add: With javascript use CamelCase style when writing variables.

    // Bad
    var my-bad-var-has-dashes
    
    // Good
    var myGoodVarNoDashes
    
    #152381
    javascriptnewbie
    Participant

    Thanks very much, sir. I appreciate you helping me.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.