Javascript ist keine objektorientierte Programmiersprache (OOP), weshalb die AUFBLICK Prozess in javascript arbeiten mit DELEGATIONSPROZESS". auch bekannt als prototypische Delegation oder prototypische Vererbung.
Wenn Sie versuchen, den Wert einer Eigenschaft von einem Objekt zu erhalten, das sie nicht hat, wird die JavaScript-Engine den Prototyp des Objekts (und dessen Prototyp, jeweils 1 Schritt darüber) seine Prototypenkette, bis die Kette bis zu null das ist Object.prototype == null (Standard Object Prototype). Wenn zu diesem Zeitpunkt eine Eigenschaft oder Methode nicht definiert ist, dann undefiniert zurückgegeben wird.
Wichtel!! Die Funktionen sind Funktionen sind Objekte erster Klasse
Funktionen = Funktion + Objekte Combo
Funktionsname.prototype = { shared SubObject }
{
// other properties
prototype: {
// shared space which automatically gets [[prototype]] linkage
when "new" keyword is used on creating instance of "Constructor
Function"
}
}
So wird mit dem new
Schlüsselwort für einige der Aufgaben, die manuell erledigt wurden, z. B.
- Manuelle Objekterstellung z.B. newObj.
- Versteckte Anleihe Erstellung mit proto (alias: Protoschwamm ) in der JS-Spezifikation [[Prototyp]] (d.h. proto )
- referenzieren und Eigenschaften zuweisen
newObj
- Rückgabe von
newObj
Objekt.
Alles wird manuell erledigt.
function CreateObj(value1, value2) {
const newObj = {};
newObj.property1 = value1;
newObj.property2 = value2;
return newObj;
}
var obj = CreateObj(10,20);
obj.__proto__ === Object.prototype; // true
Object.getPrototypeOf(obj) === Object.prototype // true
Javascript Schlüsselwort new
hilft, diesen Prozess zu automatisieren:
- wird ein neues Objektliteral erstellt, das durch
this:{}
- referenzieren und Eigenschaften zuweisen
this
- Versteckte Anleihe Erstellung [[Prototyp]] (d. h. proto ) in den gemeinsamen Bereich Function.prototype.
-
implizite Rückgabe von this
Objekt {}
function CreateObj(value1, value2) {
this.property1 = value1;
this.property2 = value2;
}
var obj = new CreateObj(10,20);
obj.proto === CreateObj.prototype // true
Object.getPrototypeOf(obj) == CreateObj.prototype // true
Aufruf der Konstruktorfunktion ohne das new-Schlüsselwort:
\=> this: Window
function CreateObj(value1, value2) {
var isWindowObj = this === window;
console.log("Is Pointing to Window Object", isWindowObj);
this.property1 = value1;
this.property2 = value2;
}
var obj = new CreateObj(10,20); // Is Pointing to Window Object false
var obj = CreateObj(10,20); // Is Pointing to Window Object true
window.property1; // 10
window.property2; // 20