Understanding JavaScript objects, prototype objects, closures

Source: Internet
Author: User

JavaScript as an object-oriented language, understanding of objects, prototypes, closures, module patterns and other technical points for becoming a qualified JavaScript programmer is very important, for many years did not write a blog, today to throw a jade, under the basic do not do the front end, but quite interested, Willing to study with you. This article is only the more important points of knowledge that I think, the coherence is not particularly good, and we progress together.

Note: Chinese is not English translation, but personal understanding.

Understanding Object-oriented

Object (object)

An object is a collection of properties and have a single prototype object. The prototype may, the null value

A collection of unordered properties whose properties can contain basic values, objects, or functions.

Prototype

object that provides GKFX properties for other objects.

An object that provides properties for other objects.

Attributes are grouped into data properties and accessor properties.

Data properties

Attribute Name

Value Domain

Description

[[Value]]

Any ECMAScript language type

The value retrieved by a get access to the property.

You can get a value from a property

[[Writable]]

Boolean

If false, attempts by ECMAScript code to change the property's [[Value]] attribute using [[Set]] would not succeed.

Default True

If False, the property value cannot be modified

[[Enumerable]]

Boolean

If true, the property is enumerated by a For-in enumeration (see 13.7.5). Otherwise, the property was said to be non-enumerable.

Default True

Whether the property value can be returned through for in

[[Configurable]]

Boolean

If false, the property of the attempts to delete is the property of the accessor property, the or change it attributes (other than [[Value]], or changing [[[writable]] to false) would fail.

The default is ture.

If it is false

Deleting a property by using Delete, redefining the property, and modifying the property to an accessor property will fail.

Object.defineproperty ( O, P, Attributes )

The DefineProperty function is used to add a own property and/or update the attributes of a existing own property of an Object. When the DefineProperty function was called, the following steps is taken:

If Type(O) is not Object, throw a TypeError exception.

Let key be? topropertykey(P).

Let desc be? topropertydescriptor(Attributes).

Perform? definepropertyorthrow(O, key, desc).

Return O.

var Bird ={    Name: "Poly"};console.log (bird.name) object.defineproperty (Bird, "name", {    writable:false,    Value: "Poly"}) Console.log ("Before:" +bird.name); Bird.name= "Lily" Console.log ("after:" +bird.name);
Accessor properties

Attribute Name

Value Domain

Description

[[Get]]

Object | Undefined

If The value is an object it must a function object.

Call this function when reading an attribute

[[Set]]

Object | Undefined

If The value is an object it must a function object.

Call this function when writing properties

[[Enumerable]]

Boolean

If true, the property was to being enumerated by a for-in enumeration. Otherwise, the property was said to be non-enumerable.

You can return properties by for-in the loop.

The default value is True

[[Configurable]]

Boolean

If false, the property of the attempts to delete is a data property, or the change of its attributes would fail.

You can delete and modify properties by using Delete.

var singal={     _ip:"192.168.2.7",     Port:1111 };object.defineproperty (Singal, "IP", {    get:function()    {         returnthis. _ IP;    },    set:function(value)    {         if(value!=1111)         {             this. _ip = "192.168.1.7"}}    )   singal.ip=1234    Console.log ("IP:" +singal.ip);
Understanding prototype Objects
function Signal () {}; Signal.prototype.ip= "192.168.0.7"=1111; Signal.prototype.connect=function() {     console.log ("Connecting ...");} var New Signal (); var New Signal ();   Singal_01.connect ();    = = Singal_02.connect); // true

Create a new function and automatically create a prototype property (pointer) for the function, which points to the prototype object of the function. By default, all prototype objects automatically get a constructor ( Constructor ) property, which contains a pointer to the prototype property.

Inch

Returns true as long as the property is accessible through the object

hasOwnProperty

Returns True only if the attribute exists in the instance

Use Delete to delete an instance property.

Prototype Object Disadvantages:

1 omitted to pass initialization parameters for constructors, all instances default values are the same

2. All instances are modified after modifying the properties of the reference data type.

Note: overriding a prototype object cuts off the connection between an existing prototype and any previously existing object instance.

Closed Package

closures are functions that have access to variables in another function scope.

funcation Add (x) {    return  funcation (y)    {        Console.log (x+y)    }}

When a function is called for the first time, an execution environment and the corresponding scope chain are created, and the scope chain is assigned a specific property [scope]

for closures, an intrinsic function adds the active object of an external function to its scope.

Module models are considered particularly important, and later will be devoted to writing an article and we discuss together.

Understanding JavaScript objects, prototype objects, closures

Related Article

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.