-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreatingObjects.js
More file actions
28 lines (24 loc) · 826 Bytes
/
creatingObjects.js
File metadata and controls
28 lines (24 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// using CONSTRUCTORS ******************************************************************
function MyClass( name ) {
this.name = name;
// embedded method declaration (this method doesn't go into the class' prototype)
this.displayName = function () {
console.log( 'Howdy, ' + this.name );
// or console.log( `Howdy, ${name}` );
}
}
// method declaration via prototype
MyClass.prototype.displayName = function () {
console.log( `Howdy, ${this.name}` );
}
const myObj = new MyClass( 'Joe Sixpack' );
// using the CLASS keyword ******************************************************************
class MyClass {
constructor( name ) {
this.name = name;
}
displayName() {
console.log( `Howdy, ${this.name}` );
}
}
const myObj = new MyClass( 'Joe Sixpack' );