A Pen by Afaq Ahmed Khan on CodePen.
Created
November 17, 2018 12:32
-
-
Save afaqahmedkhan/213663d733c61b37b8c79ea7a023025d to your computer and use it in GitHub Desktop.
Object Oriented Programming: Create a Basic JavaScript Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dog = { | |
name: 'courage', | |
numLegs: 2 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Think about things people see everyday, like cars, shops, and birds. These are all objects: tangible things people can observe and interact with.
What are some qualities of these objects? A car has wheels. Shops sell items. Birds have wings.
These qualities, or properties, define what makes up an object. Note that similar objects share the same properties, but may have different values for those properties. For example, all cars have wheels, but not all cars have the same number of wheels.
Objects in JavaScript are used to model real-world objects, giving them properties and behavior just like their real-world counterparts. Here's an example using these concepts to create a duck object:
This duck object has two property/value pairs: a name of "Aflac" and a numLegs of 2.
Create a dog object with name and numLegs properties, and set them to a string and a number, respectively.