JS Face Object Programming (ii): Attributes and closures

Source: Internet
Author: User

The previous blog explains some of the basic concepts and usage of JS objects, this blog explains the JS attributes: Public properties, private properties, privileged methods.


If you have learned Java, public properties, private properties, privileged methods (that is, methods that can access and set private properties), then let's take a look at how it's done in JS.


1. Public properties

First of all, the first layer of public meaning is accessible to all, the property of opening up is relative to the private property:

function Person (name,age) {this.name=name;this.age=age;this.getname=function () {return this.name;} This.getage=function () {return this.age;}} var josh=new person (' Josh ', ' Console.log ') (josh.name);  Joshconsole.log (Josh.getname ()); Josh

Here we define a person object called Josh, and let's create another 2 other man objects with the new method

var eric=new person (' Eric ', ' ' + '), var mongo=new person (' MONGO ', ' at '); Console.log (Eric.name)  ; Ericconsole.log (mongo.name);  Monge
Every time we new an object here, all of the code in person will be copied, resulting in a waste of memory, you can define the same class of objects common method to solve this problem, so here need prototype keyword to us.

Literally, prototype can be understood as "property to type", that is, a type of attribute, for a class of attributes, with the person defined above, that is, the person of this kind of object common method or property, we change the above code

function Person (name,age) {this.name=name;this.age=age;} Person.prototype.getname=function () {return this.name;} Person.prototype.getage=function () {return this.age;} var josh=new person (' Josh ', ' + ') var eric=new person (' Eric ', ' + '), var mongo=new person (' MONGO ', ' at '); Console.log ( Josh.getname ()); Joshconsole.log (eric.name);  Ericconsole.log (mongo.name);  Monge

Therefore, the public method or property can also be understood as a method or property added through the prototype of the constructor, where the constructor is the person, because there is no concept of class in JS, so we create a new object through the constructor, and the property method defined by prototype, shared by this class, like a static method or property in a class in Java.


2. Private properties

Based on the object-facing encapsulation feature, we do not want to expose all the variables to the outside world, so take the above person object, we put the attribute definition in the this.name is replaced by the Var name, so the public property can be accessed by outsiders into a private property, The access result for the property is also undefined

function Person (name,age) {//this.name was replaced with Var namevar name=name;var Age=age;} var josh=new person (' Josh ', ' + ') var eric=new person (' Eric ', ' + '), var mongo=new person (' MONGO ', ' at '); Console.log ( Josh.name); Undefinedconsole.log (eric.name);  Undefinedconsole.log (mongo.name);  Undefined

Why is this.name replaced by Var name, and object properties are changed from public to private? First look at the This keyword, this represents the object that the current property exists in:
function Person (name,age) {this.name=name;this.age=age;}
The above code is replaced by the following paragraph, you can see that the first definition of a person empty constructor, and then define the person object, and then add the name and the age property according to the property of the person dynamic increment

function person () {}var person=new person ();p erson.name= ' Josh ';p erson.age= ' 25 ';

So how does a private property that is replaced by Var name be implemented? Here is the design of JS to face the object of several important basic concepts, namely, scope , context , closure .

First of all, if you touch a programming language, you should understand that scopes are the variables that are currently defined to be referenced within what scope. Java is more familiar with everyone, Java is block-level, in the IF, while these blocks are defined, only in the block, out of the block is not, if you want a larger range, can be defined at the beginning of the method, for the whole method is used, but can not be used in the method, but also to define class-level, Can be referenced within the entire class.

In JS, the scope is the function level , that is, the variable defined within the function, is not referenced outside the function, the function plays the role of defining scope. In this example above, access to the Josh.name property becomes undefined.

function Person (name,age) {//this.name was replaced with Var namevar name=name;var Age=age;} var josh=new person (' Josh ', ' n '); Console.log (Josh.name); Undefined

The context is then explained, the context indicates the environment in which the function or object is located, and the environment contains variables, objects, or functions. See a picture together with the concept of a scope



For this graph, the person object can access variables in the context, objects or functions, and variables in the context cannot access the Name property of the person object, because the scope of the Name property is confined to the person object, which is the outer layer is open inside the inner layer and the external layer is closed.

Then the concept of closure is derived, the closure is a closed packet, can deny access to the outer layer, the person object is a closure, outside the package can not access the private variables, can only be accessed through certain special methods (privileged methods). Next, the privileged method is described.

3. Privileged Methods

Privileged methods are equivalent to Get/set methods that provide access and settings to private variables in a Java class

function person () {//Before the private attribute is underlined, indicating the private property var _name;var _age;this.getname=function () {return _name;} This.getage=function () {return _age;} This.setname=function (name) {_name=name;} This.setage=function (age) {_age=age;}} var josh=new person (); Josh.setname (' Josh '); Josh.setage (' + '); Console.log (Josh.getname ()); Console.log (Josh.getage ());

To this, the concept of common attributes, private properties, closures, this blog with a very small space to the closure of the concept, but it shows the essence of closure,the context in which the function is located does not have the right to access variables defined within the function, so a closure (a closed package) is formed. We have this concept, and then look at the closure of the article, it will be very useful.









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.