Last active
December 29, 2015 12:28
-
-
Save nola/7670325 to your computer and use it in GitHub Desktop.
1. Define the object type by writing a constructor FUNCTION. Use a capital letter. 2. You can create any number of Car objects by calls to new. For example, 3. mycar.year = 1993, and so on. 4. then pass in an object as a property 5. create some new Person objects 6. create some new Car objects. Pass objects rand and ken as the arguments for the …
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
//1 | |
function Car(make, model, year, owner) { | |
this.make = make; | |
this.model = model; | |
this.year = year; | |
this.owner = owner; | |
//Notice the use of this to assign values to the object's properties based on the values passed to the function. | |
} | |
//2 | |
var mycar = new Car("Eagle", "Talon TSi", 1993); | |
var kenscar = new Car("Nissan", "300ZX", 1992); //a new object that goes through the constructor | |
//3 | |
mycar.year //would be 1993, Dot notation | |
mycar["year"]// is the same, 1993 Bracket notation | |
//4 | |
function Person(name, age, sex) { | |
this.name = name; | |
this.age = age; | |
this.sex = sex; | |
} | |
//5 | |
var rand = new Person("Rand McKinnon", 33, "M"); | |
var ken = new Person("Ken Jones", 39, "M"); | |
//6 | |
var car1 = new Car("Eagle", "Talon TSi", 1993, rand); | |
var car2 = new Car("Nissan", "300ZX", 1992, ken); | |
//Notice that instead of passing a literal string or integer value when creating the new objects, | |
//the above statements pass the objects rand and ken as the arguments for the owners. | |
//7 | |
car2.owner.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment