Javascript _ javascript tips

Source: Internet
Author: User
In traditional object-oriented programming, the focus is on public and private scopes. The object attributes in the public scope can be accessed from outside the object, that is, after the developer creates an object instance, it can use its public attributes. Public, protected, and private scopes

In traditional object-oriented programming, the focus is on public and private scopes. The object attributes in the public scope can be accessed from outside the object, that is, after the developer creates an object instance, it can use its public attributes. Attributes in the private scope can only be accessed within the object. That is, these attributes do not exist in the external world. This also means that if a class defines private attributes and methods, its subclass cannot access these attributes and methods.

Recently, another type of scope has become popular, namely, protected scope. Although the rules for applications with protected scopes are different in different languages, they are generally used to define private attributes and methods, but these attributes and methods can be accessed by their subclass.

It is almost meaningless to discuss these scopes for ECMAScript, because there is only one scope in ECMAScript-Public scope. All attributes and methods of all objects in ECMAScript are public. Therefore, you must be extremely careful when defining your own classes and objects. Remember, all attributes and methods are common by default.

Many developers have put forward effective attribute scope models on the Internet to solve this problem of ECMAScript. Due to the lack of private scopes, developers have developed a statute to describe which attributes and methods should be viewed as private. This specification requires that the attribute name be prefixed with an underscore. For example:

In this Code, the attribute color is private. Remember, these underscores do not change the fact that these attributes are public properties. They only tell other developers that they should regard this property as private.

Some developers also like to use single underscores to indicate private members, such as obj. _ color.

Static scope is not static

The attributes and methods defined by the static scope can be accessed from the same location at any time. In Java, a class can have static attributes and Methods. You can access these attributes and methods without instantiating the class objects, such as java.net. URLEncoder class. Its function encode () is a static method.

Strictly speaking, ECMAScript does not have a static scope. However, it can provide attributes and methods for constructors. Remember, the constructor is just a function. A function is an object that can have attributes and methods. For example:

Here, the method alternate () is actually the sayHi function. You can call sayHi () to output "hi", or call sayHi. alternate () to output "hola", just like calling a regular function ". Even so, alternate () is also a method in the sayHi () Public scope, rather than 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.