JS Object-oriented

Source: Internet
Author: User

Most of the time we are in the process-oriented, are just the people who use the object, now learning object-oriented is to become the person to write (transform) the object. first, what is the object
    • Objects: Collections of properties and methods
    • Attribute: is actually a variable (but there is a attribution)
    • Method: is actually a function (but there is a attribution)
    • Class: A collection of objects of the same set of properties and methods
Second,
    • Declared object: var obj=new object ();
    • Direct volume, declares an empty object: Var obj={};
    • Direct volume, JSON:
    • var obj={A:"Dfsdf", B:function  () {    alert (this  . a);};

Third, newFactory mode:1, a person's class:
  • functionPerson (name,sex) {//Factory mode//classes are used to create objectsvarobj={}; This. name=name; This. sex=sex; This. showname=function() {alert ( This. name);} This. showsex=function() {alert ( This. Sex);}returnobj;}

2. Do not use new to create an object for a person
    • var Ben=person ("Ben", "false");
    • No change in class
constructor Mode:1, a person's class:
function Person (name,sex) {// constructor mode this. name=name; this. sex=sex; this. showname=function  () {alert (this. name);} this. showsex=function  () {alert (this. sex);}}

2. Create a new person's object with new

var ben=New person ("Ben", "false");
Iv. prototype prototype
    • Format: Type. prototype. properties (methods) = parameters (function functions);
      • Type: can be array\object\string, etc.
      • Properties: Customizable
    • Function: Binds a property (method) to a variable or attribute of all corresponding types, which must be called by the
v. instanceof Judgment
    • Function: Used to indicate whether an object is an instance of a particular class;
    • Format: Object instanceof Class
      • Object is arr1, str2, Array ... such as
      • Class is an Array, Object, String ... such as
Six, the prototype chainExample: Var arr=[1,2,3];alert (Arr.index);
    • Undefined is also a value
    • 1. First, you will see if ARR has the index attribute on this variable.
    • 2.Array this class (constructor) has no index this property
    • 3.Object This class has no index this property
    • 4. Return undefined and assign values to arr
Vii. Inheritance of apply && call1. Create a human: function person (name,sex) {this.name=name;   This.sex=sex;  This.showname=function () {alert (this.name); }} 2, define a worker class (see the difference between call inheritance and apply inheritance): 3, Difference:
    • When you use apply, the parameter list location must correspond to:
      • function person (name,age) {}
      • Student (name,age, sex) {}
      • When apply is passed
        • Person.apply (this,arguments);
    • When using call, the parameter list position may not correspond :
      • function person (name,age) {}
      • Student (age,name, sex) {}
      • But the location corresponds to when the call is delivered .
        • Person.call (This,name,age);
Note:Call, apply change this point
    • Call: Parameters in order
    • Apply: A second argument when an array arguments

JS Object-oriented

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.