Javascript scope usage Instructions _javascript tips

Source: Internet
Author: User

Public, protected, and private scopes

In the traditional object-oriented programming, the main focus is on public and private scopes. Object properties in a public scope can be accessed from outside the object, which means that when the developer creates an instance of the object, it can use its public properties. Attributes in a private scope can only be accessed within an object, i.e. they do not exist for the external world. This also means that if a class defines private properties and methods, its subclasses cannot access those properties and methods.

Recently, another scope has been prevalent, that is, the protected scope. Although protected scopes are applied differently in different languages, they are generally used to define private properties and methods, but these properties and methods can also be accessed by their subclasses.

It is almost meaningless to discuss these scopes with ECMAScript because there is only one scope-public scope in ECMAScript. All the properties and methods of all the objects in the ECMAScript are public. Therefore, you must be cautious when defining your own classes and objects. Remember that all properties and methods are public by default.

Many developers have put forward an effective attribute scoping mode on the Internet, which solves the problem of ECMAScript. Because of the lack of a private scope, developers have developed a protocol that describes which properties and methods should be considered private. This specification provides an underscore before and after the property name. For example:

In this piece of code, the property color is private. Remember, these underscores do not change the fact that these properties are public properties, it simply tells other developers that the attribute should be considered private.

Some developers also like to use a single underline to describe private members, such as Obj._color.

Static scopes are not static

Properties and methods defined by a static scope can be accessed from the same location at any time. In Java, classes can have static properties and methods, and you can access them without instantiating objects of that class, such as the Java.net.URLEncoder class, whose function encode () is a static method.

Strictly speaking, ECMAScript does not have a static scope. However, it can provide properties and methods to the constructor. Remember, constructors are just functions. Functions are objects, and objects can have properties and methods. For example:

Here, the method alternate () is actually a method of function Sayhi. You can call Sayhi () output "HI" as you would call a regular function, or you can call Sayhi.alternate () output "Hola". Even so, alternate () is a method in the Sayhi () public scope, not a static method.

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.