Stitcher for Podcasts

Get the App Open App
Bummer! You're not a
Stitcher Premium subscriber yet.
Learn More
Start Free Trial
$4.99/Month after free trial

Show Info

Episode Info

Episode Info: y syntax person.firstName = "Trevor"; person.lastName = "Page"; person.age = 32; person.gender = "Male"; And now we have created an Object without using a whole bunch of JSON text! Note: there are subtle differences between these two objects, but we won’t dive into those crazy details until we talk more about an Object’s prototype in a later lesson. Creating Methods in Objects There’s a great article on that sums up what Objects are in an elegant way: JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name and a value. A property’s value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. So what I want to talk about now is creating property methods. Remember that an Object can have properties, and those properties are comprised of a name and a value. For example a property name could be "gender" and a value could then be "Female". Pretty simple right? But with the addition property methods, we can have a the value of a property be a function! Let’s add a function to our person object. As always there are two ways to do this, you can either use JSON or the dot property syntax. First let’s take a look at the JSON syntax: var person = { firstName: "Trevor", lastName: "Page", age: 32, gender: "Male", goToWork: function () { alert(this.firstName + " is now going to work"); } }; Now let’s take a look at the dot property syntax: person.goToWork = function () { alert(this.firstName + " is now going to work"); } Note: With the JSON code, we needed to add the goToWork function in with the original definition of the object, so there seems to be a lot more code when using the JSON syntax… but this code will be reduced once we talk more about the subject of prototypes. Okay, so now that we’ve added a function as a property to our person object, all we need to do to “invoke” the function is this: person.goToWork(); Once we run the code above, we’ll get an alert saying "Trevor is now going to work". In Summary We’ve only hit on a few key points with respect to JavaScript Objects and there are more tutorials to come that will cover other aspects of programming with Objects in JavaScript. But so long as you can remember that Objects have properties, and properties are made up of name / value pairs, then you’ll be in a great spot to keep on learning how to use Objects in JavaScript. OH! Hey by the way, don’t forget to put your email address in the box below for some free goodies. Seriously, I’ll send you something very relevant to what you’re learning now directly to your inbox. Check out the details in the box below. The post EP36 – Objects in JavaScript appeared first on Coders Campus. ...
Read more »

Discover more stories like this.

Like Stitcher On Facebook


Episode Options

Listen Whenever

Similar Episodes

Related Episodes