Home  >  Article  >  Web Front-end  >  Principles of using JS objects

Principles of using JS objects

php中世界最好的语言
php中世界最好的语言Original
2018-06-04 10:44:081327browse

This time I will bring you the principles of using JS objects, what are the precautions when using JS objects, the following is a practical case, let's take a look.

The unique thing about JS is that nothing is sacrosanct. By default, you can modify any object you can touch. It (the parser) doesn't care whether these objects are developer-defined or part of the default execution environment - as long as they are accessible, they can be modified. This is not a problem in a project where the developer is working on it alone, the developer knows exactly what is being modified because he knows all the code. However, in a project developed by multiple people, random modification of objects is a big problem.

What are your objects

You own these objects when your code creates them. You may not have to write the code that creates the objects, but as long as it's your responsibility to maintain the code, you own the objects. For example, the YUI team owns the YUI object, and the Dojo team owns the dojo object. Even if the original author who wrote the code defining the object moves away, the respective team remains the owner of the object.

When using a JS class library in a project, you personally do not automatically become the owner of these objects. In a project developed by multiple people, everyone assumes that library objects will work as described in their documentation. If you are using YUI and modify objects in it, then this sets a trap for your own team. This is bound to cause some problems that some people may fall into.

Please remember, if your code does not create these objects, do not modify them, including:

Native objects (Object, Array, etc.)

DOM object (for example, document)

Browser Object Model (BOM) object (for example, window)

Object of the class library

All these objects above are part of your project execution environment. Since they already exist, you can use these directly or use them to build some new functionality, and you should not modify them.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Event processing in web developmentWhat are the rules

Loose coupling of the UI layer in web development

The above is the detailed content of Principles of using JS objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn