Skip to content

Instantly share code, notes, and snippets.

@corocoto
Last active November 6, 2018 12:24
Show Gist options
  • Select an option

  • Save corocoto/2870ae1255f05223f107e16fd024ae75 to your computer and use it in GitHub Desktop.

Select an option

Save corocoto/2870ae1255f05223f107e16fd024ae75 to your computer and use it in GitHub Desktop.
Определение конструктора, создавшего объект в JS
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