Js writing specifications _ basic knowledge

Source: Internet
Author: User
It is best to use the object-oriented method for js writing and packaging. js writing in two ways. The closure prototype uses JavaScript in a large number in a project. The engineering project and website development are somewhat different, js is not widely used in the projects I have come into contact with. Most clients can do things and give them to the server. js is not standardized enough, it is easy to cause the code to be difficult to read, memory leakage issues, do not pay attention to js writing methods. In website development (especially for some large websites, js outputs are very beautiful and perfect. No matter jquery, prototype frameworks, or frameworks are used, they all have their own set of good functions available)
It is best to use the object-oriented method for js writing and packaging. js writing and writing. Closure prototype.
Closure: (borrow an example)

The Code is as follows:

Function Person (firstName, lastName, age)
// Private variable:
Var _ firstName = firstName;
Var _ lastName = lastName;
// Public variables:
This. age = age;
// Method:
This. getName = function ()
Return (firstName + "" + lastName );
This. SayHello = function ()
Alert ("Hello, I'm" + firstName + "" + lastName );
Var BillGates = new Person ("Bill", "Gates", 53 );

Prototype: (borrow an example)

The Code is as follows:

// Define the constructor
Function Person (name)
This. name = name; // define a member in the constructor
// Define the method to the prototype of the constructor.
Person. prototype. SayHello = function ()
Alert ("Hello, I'm" + this. name );
// Subclass Constructor
Function Employee (name, salary)
Person. call (this, name); // call the upper-layer constructor.
This. salary = salary; // extended member
// The subclass constructor first needs to use the upper-layer constructor to establish the prototype object and implement the concept of inheritance.
Employee. prototype = new Person () // only the prototype method is required. The members of this object have no meaning!
// The subclass method is also defined on the constructor.
Employee. prototype. ShowMeTheMoney = function ()
Alert (this. name + "$" + this. salary );
Var BillGates = new Person ("Bill Gates ");
BillGates. SayHello ();
Var SteveJobs = new Employee ("Steve Jobs", 1234 );
SteveJobs. SayHello ();

The two methods have their own advantages and disadvantages. The first one looks more like a class. Setting a method for each object is a great waste, and resource recycling is not good. The second method does not look very beautiful, good performance (however, if you use the prototype framework, you can solve the structure and performance problems perfectly .)

Actually, when jquery or prototype is used, jquery uses the closure method. prototype, of course, is a prototype, and jquery is more suitable for operations on a single object, prototype is more suitable for some client controls. Actually, I prefer to use jquery in projects, and I pay more attention to prototype on websites.
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.