One of JavaScript object-oriented learning

Source: Internet
Author: User
Tags arrays wrapper hasownproperty

Review Object Oriented knowledge:

What is a wrapper object?

The base type has its own corresponding wrapper object String number Boolean Array Data

String is not an object why does it have a method?

Solution: Methods are hung under the prototype of the wrapper object Var str=new String ();

Prototype chain

The outermost layer of the prototype chain: Object.prototype

The connection between the instance object and the prototype is called the prototype chain _proto_

function Aaa () {
this.num=20; Priority is higher than NUM under the prototype
}
aaa.prototype.num=10 (Num found through the prototype chain)

Object.prototype.num=30 (outermost, lowest priority)

var p1=new Aaa ();

alert (p1.num); 20;

—————————————————–

Advanced Object Oriented

Some properties and methods of the object of surface image

hasOwnProperty: Look at the attributes below the object itself
var arr=[];
arr.num=10;
array.prototype.num2=20;
Alert (arr.hasownproperty (' num '));//true;
Alert (Arr.hasownproperty (' num2′)); False

Constructor: View the constructor of an object (can be used to determine whether it is an array, and other types)
function Aaa () {}
var a1=new Aaa ();
alert (a1.constructor);/Aaa This constructor (AAA.PROTOTYPE.CONSTRUCTOR=AAA; The program automatically generates this sentence

Determines whether array arrays (invalidated in the case of IFRAME)
Alert (Arr.constructor==array) {}

Avoid modifying constructor as follows;
function Aaa () {}

1, the first method of Www.111cn.net
Aaa.prototype.name= "Xiao Ming"; Adding a method to the prototype does not modify the constructor
2, the second method
aaa.prototype={//This will cover constructor
CONSTRUCTOR:AAA,//Here you need to specify the next Construtor (larger libraries will be specified, such as jquery)
Name: "Xiaoming"
}

' For ' cannot find the system's own properties

Instanceof: Whether the object is related to the stereotype (or it can be used to determine the array)

var arr=[];
Alert (arr instanceof Array); true; can be used to determine whether an array

function Aaa () {}
var a1=new Aaa ();
Alert (A1 instanceof Aaa)//true;
Alert (A1 instanceof Object)/true;

ToString (); Function: To turn objects into strings; (The system objects are all self-contained, and the objects written by themselves are found under object by the prototype chain)

var num=255; Alert (num.tostring (16));//Receive one parameter, the conversion; (color Rgba convert)

Use ToString to make types of judgments:
var arr=[];
Alert (Object.prototype.toString.call (arr));//[object Array]

Three ways to determine whether a variable is not an array of arrays:
Alert (Object.prototype.toString.call (arr) = = ' [Object Array] '); Best array judgment method, recommended
Alert (Arr.constructor==array) (invalidated in the case of IFRAME)
Alert (arr instanceof Array); (Failure in the case of IFRAME)

Encapsulate
 
JavaScript is an object-based (object-based) language, and almost all of the things you encounter are objects. However, it is not a true object-oriented programming (OOP) language because its syntax has no class (class).
So, what should we do if we want to encapsulate the property and method (methods) into an object and even generate an instance object from a prototype object?
First, the original schema of the generated object Www.111cn.net
Suppose we look at a cat as an object, and it has two properties, first name and color.
var Cat = {
Name: ',
Color: '
}
Now we need to generate two instance objects based on the specification (schema) of this prototype object.
var cat1 = {};//Create an empty object
Cat1.name = "hairy";//According to the properties of the prototype object
Cat1.color = "yellow";
var cat2 = {};
Cat2.nam E = "Er mao";
Cat2.color = "BLACK";
OK, so that's the simplest package, which encapsulates two attributes in one object. However, there are two disadvantages to this writing, one is that if you generate more than a few instances, it is very troublesome to write, and the second is between the example and the prototype, there is no way to see what connection.
Ii. improvements to the original schema
we can write a function that solves the problem of code duplication.
Function Cat (name,color) {
Return {
Name:name,
Color:color
}
}
then generate the instance object, which is tantamount to calling the function:
var CA T1 = Cat ("hairy", "yellow");
var cat2 = Cat ("Er Mao", "Black");
The problem with this approach remains that there is no intrinsic connection between CAT1 and CAT2 and that they are not instances of the same prototype object

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.