The idea invented by Netscape comes from the functional language Schema and Self, a little closer to Python.
Features: weak type, dynamic parsing, the function is an object, and the object is based on Prototype
The first three features are easy to understand, and the last one should be explained in detail.
The most basic Best Practice: Use Fuction and objects in large programs! Do not use sequential account-based programming.
The easiest way to create an object is:
Var myObject = new Object ();
JavaScript objects are essentially associated arrays.
TIPS: Pay attention to the difference between using functionName () and functionName when assigning a function to a variable. The former is to pay the function result to the variable, and the latter is to pay the function reference to the variable.
You can use JSON to create and modify JavaScript objects. JavaScript can also modify the objects created in JSON.
Constructor/class/prototype attribute
JavaScript also has the concepts of objects and classes, but it does not have the built-in inheritance concept. In fact, every JavaScript Object is an instance of the same base class. This base class has the ability to bind member fields and functions with itself at runtime.
Secure alternative: prototype attribute
Prototype is an attribute of a JavaScript Object, and there is no peer in the OO language. Functions and attributes can be associated with the prototype of the constructor. The prototype and new keywords work collaboratively. When the new function is called, all attributes and methods of the function prototype are appended to the result object.
Java code
Function MyObject (name, size ){
This. name = name;
This. size = size;
}
MyObject. prototype. tellsize = function (){
Alert ("size of" + this. name + "is" + this. size );
}
Var myObj = new MyObject ("tiddles", "7.5 meters ");
MyObj. tellSize ();
Function MyObject (name, size ){
This. name = name;
This. size = size;
}
MyObject. prototype. tellsize = function (){
Alert ("size of" + this. name + "is" + this. size );
}
Var myObj = new MyObject ("tiddles", "7.5 meters ");
MyObj. tellSize ();
Note: Only after the constructor is declared can prototype be referenced. The object's grace inherits the things that have been attached to prototype before the constructor is called. Prototype can be modified between two calls to the constructor, and anything can be appended, not just a function, to prototype.
Using prototype to define behavior similar to a class for JavaScript objects is a safe and reliable path.
Extended built-in class
In Web browsers, some core classes can be extended through the prototype mechanism.
Java code
Array. prototype. indexOf = function (obj ){
Var result =-1;
For (var I = 0; I <this. length; I ++ ){
If (this [I] = obj ){
Result = I;
Break;
}
}
}
Array. prototype. indexOf = function (obj ){
Var result =-1;
For (var I = 0; I <this. length; I ++ ){
If (this [I] = obj ){
Result = I;
Break;
}
}
}
Prototype inheritance
JavaScript is not implemented locally, but there are some clever workarounds.
Reflection of JavaScript objects
Type used to discover objects
Event Processing and function Context
When the Dom element Event handler function is declared programmatically, even if no parameter is assigned to the function, when a Dom element is clicked, the Event object acts as a parameter for function calling and the element itself acts as the context object.
If you want the event processing function to reference the model object it is attached to, you can use either of the following methods:
1. Reference a model by name
2. Add a model to the Dom Node
From: Grocery Store