Home  >  Article  >  Web Front-end  >  js writing specifications_basic knowledge

js writing specifications_basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:33:371018browse

A large amount of js is used in a project. Engineering projects are somewhat different from website development. In the engineering projects I come into contact with, js is generally not used enough. Most of the client-side tasks are left to the server, and there is not enough use of js. Standardization can easily lead to code that is difficult to read and memory leaks, and carelessness in the way of inputting and writing js can occur. And in website development (especially some large websites, the js output is very beautiful and perfect, no matter whether you use jquery, prototype framework, or no framework, you have your own good set of things available)
js input and writing is best. The object-oriented method uses the class direction to wrap js and write two methods. Closure prototype
Closure: (an example borrowed)

Copy code The code is as follows:

function Person(firstName, lastName, age)
{
//Private variables:
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: (an example borrowed)
Copy code The code is as follows:

  //Define the constructor
function Person(name)
{
this.name = name ; //Define members in the constructor
};
//The method is defined on 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-level constructor
this.salary = salary; //Extended members
};
//The subclass constructor first needs to use the upper-level constructor to create a prototype object and implement the concept of inheritance
Employee.prototype = new Person() //Only the methods of its prototype are needed, the members of this object have no meaning!
//Subclass methods are also defined above 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( ; , it doesn’t look very beautiful, but the performance is very good (but if you use the prototype framework, you can perfectly solve the structure and performance problems.)
Actually, my little experience on whether to use jquery or prototype It is jquery that uses closures, and prototype is of course a prototype. jquery is more suitable for operating on a single object, and prototype is more suitable for making some client controls. In fact, I prefer to use jquery in projects and focus more on prototype on the 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