Hey you guys, two months did not write technical blog. As an ideal, aspiring programmer, two months do not write technical blog, really should play. Diligence, desolate in the leisure. Journeys by chance, destroyed in the following. Diligence is essential, in the future to develop the habit of at least one blog a week. Well, no more nonsense, the book belongs to the story. Today, when you are working on a project, you encounter situations where you need to create JavaScript objects. So Bing a foreigner wrote about 3 kinds of JavaScript objects to create the article, read and followed the code. The feeling method is very good, here to share with you.
First, use the function to create the object:
//Defining ObjectsfunctionAnimal (type) { This. Name= ""; This. type=type; This. introduction=function(){ return"My name is:" + This. name+ ", I belong to" + This. Type;}}varAnimal=NewAnimal ("poultry");//instantiate the object we created aboveAnimal.name= "Little Red"; Alert (Animal.introduction ()); //Call its introduction function (at this point, the page pops up: My name is Little red, I belong to poultry);
This method, everyone must be very familiar with. However, using this method can result in a loss of performance. Here, we instantiate the object with the new key child. In fact, the new key son is doing two things. One, defines an anonymous method (Animal). Second, call it. This is not as efficient as the approach we are going to introduce next.
Second, the use of object literals (literals):
I do not know the translation is right, I will be the original address to tell you, interested can see the original.
// define object book= {name: "Red Mansions" Typ E: literary works function Span style= "color: #000000;" > () { return : "I am Cao Xueqin's child!" // Call the object method, and the page appears: I am Cao Xueqin's child. book.name= "slam Dunk"; // Modify object Properties alert (book.name); // at this point, the page will pop up: Slam dunk
Believe that you see the code, you should understand why this method will be more efficient. Because, it's equivalent to defining a JavaScript global variable. We can use it directly, and we don't need to instantiate it. But it looks weird. Well, here's the solution. Let's take a look at the third method.
three, single-case mode (Singleton using a function):
translation into a singleton pattern may not be too good. Let's look at the code first:
// Defining Objects var gender=Newfunction() { this. type= "Girl"; this. speaking=function() { return "I am" +this. Type;}} Alert (gender.speaking ();) // Use Object at this point the page appears: I'm a girl.
Do you look at this piece of code, is it similar to our method? However, it can work like method one. Method One, once the object is used, the object will be created once. This method, once the object is created, can be used permanently. So, this approach is very similar to the singleton pattern in design mode.
Original address: http://www.phpied.com/3-ways-to-define-a-javascript-class/
3 ways to create and invoke JavaScript objects