Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Organizing and Simplifying JavaScript with Object Literals - 12

Embed code for this video

Copy the code above to embed our video on your website/blog.

Close

Video format

Option selected may change based on video formats available and browser capability.

Close

Download

Right click “Save as…”

Developers coming from Object Oriented Programming backgrounds may notice similar constructs (objects, the new keyword, etc.), but in JavaScript these ideas are implemented much differently. This video demonstrates the creation of simple objects called "object literals" to keep related properties and functions (methods) together in one structure.  Finally, Bob demonstrates using constructors and the new keyword to show how different these ideas are from other programming languages.

Tag:

Follow the Discussion

  • Re-opening this closed thread ...

  • Lee WoodLee Wood

    You say that "status:" and "rescueBatman:" are variables, so why do you use ":" to define the variable, why can't you use "="?

    also you don't explain object literal very well, this is supposed to be for Absolute Beginners and you were speaking about it as if we have previous knowledge like with C# and it sounds very complicated so this lesson really did nothing for me while the other previous ones have been good.

  • @Lee Wood: Hey Lee, sorry this video didn't do a very good job of explaining it.  If / when I ever re-do this video, I'll pay more attention to how I go about this.  Thanks for the feedback.

    If I said that those object members are "variables", that was a misstatement and I apologize.  They are MEMBERS of an object.  Objects can have properties (attributes) and methods (functionality).  In this case, "status" is a property / attribute, and "rescueBatman" is a method.  Both are members of the whole, of the new object.  We set a reference to that new object using the equals = operator.

    Why use a colon : and not an equals = operator? Here are two examples that are functionality equivalent:

    var myObject = {
        firstProperty:'some value',
        secondProperty:'some other value'};

    .. is functionally the same as:

    var myObject = new Object();
    myObject.firstProperty = 'some value';
    myObject.secondProperty = 'some other value';

    You'll see the former syntax (with the colon : ) more often in JavaScript because it is so compact. In fact, as you learn more, you'll that this is the defacto way to pass data between servers and web browser clients using this exact style of syntax, called JSON (IavaScript Object Notation) BECAUSE it is so compact. More about that in an upcoming video.

    Hope that helps? Please feel free to follow up ... we'll get to the bottom of this! Smiley

  • Bill CooperBill Cooper

    Hi Bob,

    Great series that has been a break through for me.
    I have a problem similar to our poster above in that I'm a total non programmer who is "undefined" at this point. In your last example in this video, you state that we want our car to have a make property, and we assigned it to the temp variable "make" that was passed into the constructor. You also stated that you hoped it didn't throw anyone.

    I'm tossed :). Regarding

    function car(make, model, year){
    this.make = make;
    this.model = model;
    this.year = year;

    I hope I am not asking to much if I could inquire about a little deconstruction on that point? I'm one of those that over-thinks everything down to the nub, so I've gone and confused myself. Especially because I am completely unfamiliar with the word/object/command "this", what it does, and how it is used.
    (I understand it is a complex power word, but an overview of it's use in the context of just this instance would help)
    To add to my confusion, I'm not sure which instance is the temp variable (this.make?), and when you stated that we passed it into the constructor, I'm wondering if you are referring to the first line in the braces, or if you are saying we are passing it into the finished total block of constructor code.

    Oddly enough, I see clearly that this works, I'm just not so sure on the how/why in the block mentioned above.

    Thank you so much for opening my eyes so far.

    Bill

  • Bill CooperBill Cooper

    Regarding "this" usage, the following tutorial about covers nicely:

    http://net.tutsplus.com/tutorials/javascript-ajax/fully-understanding-the-this-keyword/

    Thanks again.

    Bill

Remove this comment

Remove this thread

close

Comment on the Post

Already have a Channel 9 account? Please sign in