Last active
November 6, 2018 12:24
-
-
Save corocoto/2870ae1255f05223f107e16fd024ae75 to your computer and use it in GitHub Desktop.
Определение конструктора, создавшего объект в JS
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
| var cadiParams = { | |
| make: "GM", | |
| model: "Cadillac", | |
| year: 1955, | |
| color: "tan", | |
| passengers: 5, | |
| convertible: false, | |
| mileage: 12892 | |
| }; | |
| var cadi = new Car(cadiParams); | |
| //оператор instanceof возвращает true, если объект был создан указанным конструктором | |
| if(cadi instanceof Car){ | |
| console.log("Congrats, it's a Car!"); | |
| } | |
| function Car (params){ | |
| this.make=params.make; | |
| this.model=params.model; | |
| this.year=params.year; | |
| this.color=params.color; | |
| this.passengers=params.passengers; | |
| this.convertible=params.convertible; | |
| this.mileage=params.mileage; | |
| this.started=false; | |
| this.start=function (){ | |
| this.started=true; | |
| }; | |
| this.stop=function () { | |
| this.started=false; | |
| }; | |
| this.drive=function () { | |
| if (this.started) { | |
| console.log(this.make + " " + this.model + " goes zoom zoom!"); | |
| } else { | |
| console.log("Start the engine first"); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment