SORU
22 Mayıs 2009, Cuma


Kullanmak için en iyi olan: sattığınız ya da örneğin?

Özellikle benim durumumda:

callback instanceof Function

ya

typeof callback == "function"

ne fark eder bile fark eder mi?

Ek Kaynak:

JavaScript-Bahçe typeof vs instanceof

CEVAP
8 Temmuz 2011, Cuma


Özel türler için: instanceof kullanın

var ClassFirst = function () {};
var ClassSecond = function () {};
var instance = new ClassFirst();
typeof instance; // object
typeof instance == 'ClassFirst'; //false
instance instanceof Object; //true
instance instanceof ClassFirst; //true
instance instanceof ClassSecond; //false 

Basit türler için yerleşik: typeof kullanın

'example string' instanceof String; // false
typeof 'example string' == 'string'; //true

'example string' instanceof Object; //false
typeof 'example string' == 'object'; //false

true instanceof Boolean; // false
typeof true == 'boolean'; //true

99.99 instanceof Number; // false
typeof 99.99 == 'number'; //true

function() {} instanceof Function; //true
typeof function() {} == 'function'; //true

Karmaşık türleri için yerleşik instanceof kullanın:

/regularexpression/ instanceof RegExp; // true
typeof /regularexpression/; //object

[] instanceof Array; // true
typeof []; //object

{} instanceof Object; // true
typeof {}; //object

Sonuncusu biraz zor

typeof null; //object

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • FamilyFeud

    FamilyFeud

    22 AĞUSTOS 2006
  • Jonnyriddlin1

    Jonnyriddlin

    4 Ocak 2007
  • spyib

    spyib

    9 Ocak 2007