JS-oriented (based on) object programming

Source: Internet
Author: User
Tags garbage collection ming
Introduction to JS-oriented (based on) object programming

to create an object for a template with a class (prototype object)


Zhang Lao tai cat Problem

//traditional method more troublesome
var cat1_name= "small white";
var cat1_age=3;
var cat1_color= "White";

Solution: Put the cat's attribute set, create a new data class (prototype Object/Class). Use object-oriented approach to solve the above problem. //
Here is a cat class

function Cat () {
}
//If you use
//cat ();//function
var cat1=new cat ();//class
// At this time CAT1 is an object (instance)
cat1.name= "Small white";
cat1.age=3;
cat1.color= "White";
From the above code we can see that the
properties of objects in//1.js can be dynamically added.
//2. property is not restricted.

Window.alert (cat1.name+cat1.age+cat1.color+cat1.hobby);/No defined object pops up undefined




Streamlined as follows:


There are five different ways to create objects:

Use constructors to define classes (prototype objects)

Example:

In JS everything is Object function Person
() {}
Window.alert (person.constructor);//class is also object, is created by function of the object
var a= New person ();
Window.alert (a.constructor);//a the constructor of the object instance
Window.alert (typeof a); What type of//a is

var b=123;
Window.alert (b.constructor);
Window.alert (typeof b);

var c= "123";
Window.alert (c.constructor);
Window.alert (typeof c);

How to tell if an object instance is a person type.

if (a instanceof person) {
	Window.alert ("A is person Ok1");
}

if (A.constructor==person) {
    Window.alert ("A is person ok2");
}

Two ways to access the properties of an object
function person () {}
var p1=new person;
P1.name= "shunping";
alert (p1.name);
Alert (p1["name"]);
var val= "na" + "Me";
Alert (P1[val]);

Run Result: 10 name small white name small white

Operation schematic diagram:

JS garbage collection mechanism:

Memory by the JS engine management, JS engine is a part of the browser


Number of references 0 content 0 garbage collection

Description of object reference problem-----diagram

①js passively frees object memory:

Passive embodiment even if the reference number is 0, and do not know when it will be recycled, may be insufficient memory time

JS free memory ①----passive
 function person () {}
 var a=new person ();
 a.age=10;
 A.name= "Xiao Ming";
 var b=a;
 B.name= "Small white";
 Window.alert (b.age+ "name" +b.name+ "name" +a.name);
So far, two of the contents of the heap are quoted
B=null;
Window.alert (a.age+ "name" +a.name);
Window.alert ("b.age=" +b.age);
At this time the reference
a=null;

②JS also provides a way to proactively free object memory: Delete

JS free memory ②----Active  Delete
function person () {};
var a=new person ();
a.age=10;
A.name= "Xiao Ming";
var b=a;
B.name= "Small white";
Window.alert (b.age+ "1 names" +b.name);
Delete a.age;//Remove the properties of object A (Cannot delete object, only delete attribute)
//a and B are pointing to the same place, so B.age will also be deleted.
Window.alert (b.age+ "2 Names" +b.name);


Run Result: xiaoming 200

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.