In meinem speziellen Fall:
callback instanceof Function
または
typeof callback == "function"
Spielt das überhaupt eine Rolle, was ist der Unterschied?
Zusätzliche Ressource:
JavaScript-Garten typeof gegen instanceof
In meinem speziellen Fall:
callback instanceof Function
または
typeof callback == "function"
Spielt das überhaupt eine Rolle, was ist der Unterschied?
Zusätzliche Ressource:
JavaScript-Garten typeof gegen instanceof
var newObj = new Object;//instance of Object
var newProp = "I'm xgqfrms!" //define property
var newFunc = function(name){//define function
var hello ="hello, "+ name +"!";
return hello;
}
newObj.info = newProp;// add property
newObj.func = newFunc;// add function
console.log(newObj.info);// call function
// I'm xgqfrms!
console.log(newObj.func("ET"));// call function
// hello, ET!
console.log(newObj instanceof Object);
//true
console.log(typeof(newObj));
//"object"
Nach Angaben von MDN-Dokumentation über typeof Objekte, die mit dem Schlüsselwort "new" instanziiert werden, sind vom Typ "object":
typeof 'bla' === 'string';
// The following are confusing, dangerous, and wasteful. Avoid them.
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String('abc') === 'object';
Während Dokumentation über instanceof weist darauf hin:
const objectString = new String('String created with constructor');
objectString instanceOf String; // returns true
objectString instanceOf Object; // returns true
Wenn man also z.B. prüfen will, ob etwas eine Zeichenkette ist, egal wie sie erstellt wurde, wäre es am sichersten, wenn man instanceof
.
CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.