Object-oriented 1 in Java script

Source: Internet
Author: User

Object-oriented in Java script

    1. 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.

    1. 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>

    1. 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

?

?

?

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.