Last active
November 12, 2019 00:25
-
-
Save emyrold/0e5fd3890d98aed930a78e78d9b8e904 to your computer and use it in GitHub Desktop.
This file contains 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
/* Ok, here are my summary notes for the different ways to create multiple instances of an Object. | |
1. class.create | |
2. constructor function with 'new' | |
3. Object.create | |
4. factory function | |
*/ | |
// 1. ServiceNow class.create . *** object written using Pascal notation (first letter uppercase) | |
var Person = Class.create(); | |
Person.prototype = { | |
initialize: function (first, last) { | |
this.first = first || 'Default'; | |
this.last = last || 'Default'; | |
this.getFullName = function () { | |
return this.last + ', ' + this.first; | |
} | |
// return this; is implied | |
}, | |
getFormalName: function () { | |
return this.last + ', ' + this.first; | |
}, | |
type: 'Person' | |
}; | |
/// how to use/ | |
var sally = new Person('Sally', 'Smith'); | |
sally.getFullName(); | |
sally.getFormalName(); | |
// 2. constructor function using 'new' keyword . *** object written using Pascal notation (first letter uppercase) | |
function Person(first, last) { | |
this.first = first || 'Default'; | |
this.last = last || 'Default'; | |
this.getFullName = function () { | |
return this.first + ' ' + this.last; | |
} | |
// return this; is implied | |
} | |
// how to use | |
var sally = new Person('Sally', 'Smith'); | |
sally.first; | |
sally.last; | |
sally.getFullName(); | |
// 3. Object.create() *** object written using Camel notation (first letter lowercase) | |
var person = { | |
first: 'Default', | |
last: 'Default', | |
getFullName: function () { | |
return this.first + ' ' + this.last; | |
} | |
}; | |
// how to use | |
var sally = Object.create(person); | |
sally.first = 'Sally'; | |
sally.last = 'Smith'; | |
sally.getFullName(); | |
// 4. factory function . *** object written using Camel notation (first letter lowercase) | |
function person(first, last) { | |
return { | |
first: first || 'Default first', | |
last: last || 'Default last', | |
getFullName: function () { | |
return this.first + ' ' + this.last; | |
} | |
}; | |
} | |
// How used | |
var sally = person('Sally', 'Smith'); | |
sally.first; | |
sally.last; | |
sally.getFullName(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment