www.webdeveloper.com
Results 1 to 9 of 9

Thread: New guy learning Javascript

  1. #1
    Join Date
    Aug 2014
    Posts
    2

    New guy learning Javascript

    Hello,

    I've been through PHP, MySql, HTML5, C,, and now I'm into Javascript!

    Question.

    When you make a variable equal to this:

    var SoWide = {

    Even: false,

    Odd: false,

    From: 0,

    To: 1

    }

    What does the Even:false mean? Is it a bolean? Is the 'From:0, To:1 setting it to both be false? I came across this example and I'm not sure what it is saying.

    Thanks

    Deaf

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    SoWide is an object. Even, odd, from, to are the keys. False, 1 and 0 are the values. They're all independent. Objects are very useful in javascript (in fact, pretty much everything in javascript is an object) - if you are going to learn the language, I would highly recommend you studying them in depth.

  3. #3
    Join Date
    Aug 2014
    Posts
    2
    Ok. So if you have a:

    var f = SoWide;
    var g = SoWide;

    // that makes new objects f and g with the same properties as SoWide, right?
    //and below that loads the values into the keys of objects f and g.
    f.Even = true;
    f.From = 2;
    f.To = 100;
    g.Odd = true;
    g.From = 51;
    g.To = 71;
    // right?

    Thanks,

    Deaf

  4. #4
    Join Date
    Mar 2009
    Posts
    491
    // that makes new objects f and g with the same properties as SoWide, right?
    No. It creates new references to the same object. Assuming the existence of your code defining soWide, and f = soWide changing any property in one changes the properties in both, because actually there is no "both" at all.

  5. #5
    Join Date
    May 2014
    Posts
    897
    Hence the keyword "new"

    Code:
    var
      f = SoWide, // same as a pointer to the original
      g = SoWide, // same as a pointer to the origianl
      h = new SoWide; // new instance of the object with its own unique properties
    JS objects are ALWAYS objects, there is no prototyping via classes; kind of like all objects are BOTH variables AND classes at the same time.

    Which I initially found confusing when I started with JS back in the '90's coming from a ASM, Pascal, Modula, Oberon, Smalltalk and C background.
    Last edited by deathshadow; 08-12-2014 at 02:49 AM.
    Java is to JavaScript as Ham is to Hamburger.

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Quote Originally Posted by deathshadow View Post
    Hence the keyword "new"

    Code:
    var
      f = SoWide, // same as a pointer to the original
      g = SoWide, // same as a pointer to the origianl
      h = new SoWide; // new instance of the object with its own unique properties
    I agree with the new keyword to create a new instance, but that cannot be done with an object object, you would require a function object instead:

    Code:
    function SoWide()
    {
    	this.Even = false;
    	this.Odd  = false;
    	this.From = 0;
    	this.To   = 1;
    }
    
    var
    	f = new SoWide(), // first instance of the object
    	g = f, // same as a pointer to "f"
    	h = new SoWide(); // second instance of the object
    Last edited by bionoid; 08-14-2014 at 06:58 PM.
    JavaScript: Learn | Validate | Compact | bionoid

  7. #7
    Join Date
    May 2014
    Posts
    897
    Quote Originally Posted by bionoid View Post
    I agree with the new keyword to create a new instance, but that cannot be done with an object object, you would require a function object instead
    Technically the correct terms for that is a literal object and a constructed object respectively. The 'function' is called a constructor, and is indeed the only way of doing it... though there are TWO ways to build constructors:

    function ourObject() {

    or

    var ourObject = function() {

    Either will work. The only time it doesn't work is with literal notation:

    var ourObject = {

    That will fail as there's no 'constructor' to be run.

    Though it does make me wish JS had REAL objects with actual classes, constructors and destructors -- same for PHP or even C++ -- hell, any C syntax family language that objects were shoe-horned into. They all have really half-assed object implementations; at least compared to Object Pascal, Modula, Oberon, or SmallTalk.

    If nothing else it would be nice to stop saying "this" all the blasted time, but I guess they figured web developers are too stupid to master scope or something... though that's evident by the people ranting and raving about the "evils" of "with".
    Java is to JavaScript as Ham is to Hamburger.

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Quote Originally Posted by deathshadow View Post
    If nothing else it would be nice to stop saying "this" all the blasted time, but I guess they figured web developers are too stupid to master scope or something... though that's evident by the people ranting and raving about the "evils" of "with".
    Years of JS conditioning has numbed my worry about "this", in fact I kinda love it. "with" is practically useless now thanks to strict mode... my past self would have enjoyed its use as it would have allowed a nicely malleable dynamic scope.
    JavaScript: Learn | Validate | Compact | bionoid

  9. #9
    Join Date
    Aug 2014
    Posts
    1
    thanks...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles