JS Object-Oriented programming

Source: Internet
Author: User

1. JavaScript is an object-based (object-based) language, and everything you encounter is almost always an object.

2. Because there is no classin JavaScript, someone has made the Class a prototype object.

function Cat () {

}

Cat ();// function

var cat1=new Cat (); class

The properties of an object in JS can be added dynamically;

Classes (prototype objects)-- How to customize classes (prototype objects) and objects:

1, factory method -- use new object to create an object and add attributes;

2. Use constructors to define classes (prototype objects)

3. Using prototype

4, the structure function and the prototype mix way

5. Dynamic Prototyping Mode

Basic syntax

function class name / prototype Object () {

}

function person () {}

var a=new person ();

Window.alert (A.constructor); constructor for a object instance

Window.alert (typeof a); What is the type of a?

var b= "1234";

Window.alert (B.constructor); constructor for a B object instance

Window.alert (typeof B); What is the type of B?

How can I tell if an object instance is a person type?

if (a instanceof person) {

Window.alert ("A is person");

}

if (A.constructor==person) {

Window.alert (' A is person ');

}

Object ----Function Special Description:

1, in JS Everything is the object

2, the Class (prototype object) is actually an object, it is actually an instance of the Function class, through the class we can create our own object instances.

Object instance --- how to access (use) member variables of an object instance

(1), object instance name . Property name

(2), object instance name . [" Attribute name "];

var a=90; Global Variables

function Test () {

a=900; in a function, a variable thatuses a global variable if it is not with var

If you take var , you define a new a variable in test () .

}

Test ();

Window.alert (a);

The use of this in JS : Which object instance invokes the function of this , then This represents an instance of which object.

〉〉 This cannot be used outside of the class definition and can only be used in methods defined by the class. If used externally, the caller is no longer an instance of the class.

function person () {

var name= "abc"; The var modifier name is private and can only be used internally

var age=90;

This.name2= "ABC2"; This.name2 indicates that the name2 property is public

This.show=function () {// this is A public method of the person class (privileged method)

Window.alert (name+ "" +age);

}

function Show2 () {// this is A private method of the person class

}

}

var p=new person ();

Window.alert (p1.name= "" +p1.age+ "" +p1.name2 "); Error

p1.name->undefined p1.age->undefined P1.NAME2->ABC2

There are two ways to add a method to an object:

The first type:

function class name {

This. Properties ;

}

Var object name =new class name ();// Create object

function function name () {

Execution

}

The name of the object . Property name = Name of function ; // this is equivalent to assigning the function to the object name . Property name,

at this point, the property name represents a function

The name of the object . Property name ;

The second type:

The name of the object . Property name =function ( parameter list ) {

}

Invocation: Object name . Property name ( actual parameters );

JS Object-Oriented programming

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.