Object-oriented in Java script
- Object
An object is the basic data type of JavaScript, which is a composite value that aggregates many key-value pairs together. An object can be considered an unordered collection of attributes, each of which is a name/value pair. The property name is actually a string. We can think of the object as a string-to-value mapping.
- Creating objects
The example code is as follows:
<script type="Text/javascript" language="javascript">
???? // Create an Object o has two attributes x y
???? ???? var o={
???????????? X:10 // Note: name / Multiple name value pairs are separated by commas
???????????? y:// The entire mapping table is enclosed in curly braces.
???????? };
???????? var o1={
????????} // Create an empty object
????????
???????? // The values in the object can refer to the values of other objects
???????? var o2={
???????????? X:o.x-1,
???????????? Y:o.y
???????????? Author:{
???????????? ???? fristname:"Lisi",
???????????????? LastName:"Zhangsan"
???????????? }
????????}
????????
???? </Script>
- Create object with new
var o3=new Object ();// Create an empty object and {} same as
???????????? var a=new array ();// Create an empty array and [] the same
???????????? var d=new date ();// Create an object that represents the current date
???????????? var r=new RegExp ("JS");// Create a RegExp object for pattern pattern matching
?
?
?
- Prototype
Each object is inherited from the prototype.
Get a reference to a prototype object through JavaScript code Object.prototype
The prototype of the object created by the keyword new and the constructor call is the value of the prototype property of the constructor
?
So objects created with the {} object created by new object also inherit from Array.prototype, and the prototype of the object created by New Date () is Date.prototype.
?
Object.prototype no prototypes.
4. Create an object from Object.create ()
Object.create () is a static function, not a method that is provided to an object for invocation.
Simply pass in the desired prototype object.
var a1=object.create ({x:ten, y:);
?
Two Setting of Object Properties
The value of the property can be taken by point. or square brackets [] operator.
Note: You must have an expression in square brackets that evaluates to a string
var 0_x=o2.x;
???????? var o_author=["author"];
You can also set property values for an object in this way
???????? o2.x=ten;
???????? o2["y"]=;
Third, Function object
You can use the Function keyword to define a function and specify a function name for each function, which is called by the name. When JavaScript interprets execution, the function is maintained as an object, which is the function object.
function MyFunction (A, b) {
???? return a+b;
}
Equivalent to
var myfunction = new Function ("A", "B", "Return A+b");
?
Other than that
var i = function (A, b) {
???? return a+b;
}
This is done immediately after the function is created.
?
Again, function FuncName () {} is equivalent to
???? var funcName = function () {}
?
?
?
?
?
?
?
?
?
?
Object-oriented 1 in Java script