cachaito
1/1/2015 - 1:41 PM

Sprawdzanie zależności za pomocą operatora instanceof

var str = 'someString';                 //tworzy prosty łańcuch znaków
var str2 = new String('anotherString'); //tworzy object String
var str3 = String('anotherString');     //ciekawostka, także tworzy prosty łańcuch znaków

str instanceof String; //false
str2 instanceof String; //true

//2 sposoby na sprawdzenie konstruktora
var Person = function(name) {
  this.name = name;
};

var bartek = new Person('Bartek');
//#1
bartek instanceof Person; //true
bartek instanceof Object; //true
//#2
Person.prototype.isPrototypeOf(bartek); //true
Person.isPrototypeOf(bartek); //false!