Js is widely used in a project. There are some differences between engineering projects and website development. js is not widely used in engineering projects that I have come into contact with. Most clients can do things and hand it over to the server, in addition, js is not standardized enough, and it is easy to cause problems of code reading and Memory leakage. js writing methods are not paid attention. 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)
Copy codeThe 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)
Copy codeThe 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.