js create classes and objects

2011-05-24  来源:本站原创  分类:Web  人气:71 

js create classes and objects in several ways:

1, the most primitive manner:

var car = new Object();
car.color = "red";
car.name = "baoma";
car.showColor = function(){

   alert(this.color);
}

Create a car object has two properties: name, color a Method: showColor

Disadvantages: can only create an object

2, the factory method

function createCar() {
  var car= new Object;
  car.color = "blue";
  car.name= "baoma";
   car.showColor = function() {
    alert(this.color);
  };
  return  car;
}

var car1 = createCar();
var car2 = createCar();

The same way as the original, create car objects

Is superior than the original way: You can create multiple car object

Disadvantages: All object properties and methods are the same

3, the constructor method

function createCar(color,name) {
   this.color = color;
   this.name= name;
   car.showColor = function() {
    alert(this.color);
  };
 }

var car1 = createCar("blue","baoma");
var car2 = createCar("red","benci");

Disadvantages:

Generating function will repeat constructor for creating a separate version of each object

4, the prototype method:

function Car() {
}
Car.prototype.color = "blue";
Car.prototype.name = "baoma";
Car.prototype.showColor = function() {
  alert(this.color);
};

Code:

First: define a constructor (Car), which without any code

Second: the use of structural properties and function prototype

When you call the new Car (), the prototype will be immediately given all the attributes of objects created, that are stored in an instance of Car is to point showColor () function pointer, semantic sense: all of the attributes that belong to a Object

The problems the prototype:

1, can not use the way of passing parameters to the property assignment

2, when the attribute points to an object rather than function, multiple instances of the object is rarely shared

5, mixed constructor / prototype approach

function Car(color,name) {
   this.color = color;
   this.name= name;

  };
 }

Car.prototype.showColor = function() {
  alert(this.color);
};

Using this approach, the basic and other languages ​​to create a kind of object.

This approach is most commonly used

This way: Even if all the functions only created once, and that each object has its own instance of the object properties

6, the prototype can be applied in this way the dynamic mode

function Car(color,name) {
  this.color = color;
  this.name = name;
  if (typeof Car._initialized == "undefined") {
    Car.prototype.showColor = function() {
      alert(this.color);
    };

    Car._initialized = true;
  }
}

The method uses the flag (_initialized) to determine whether any of the methods given to the prototype. The method to create and assign only one

The most common, best to use is the fifth, six, but the sixth perfect number

相关文章
  • js create classes and objects 2011-05-24

    js create classes and objects in several ways: 1, the most primitive manner: var car = new Object(); car.color = "red"; car.name = "baoma"; car.showColor = function(){ alert(this.color); } Create a car object has two properties: name,

  • Several JavaScript methods defined in classes and objects 2011-01-05

    Several JavaScript methods defined in classes and objects http://www.nowamagic.net/javascript/js_MethodsToCreateClassObject.php A friend recently asked me if I came across "hoisting" the problem. That all the variables in js in the statement are

  • Understanding Classes and Objects (Want Want teacher) 2010-03-29

    Class and object theory to explain: What is the class of a fine day today, sunny, and we have afternoon classes, and I feel super good. The way in to school, you met a woman to see her in that moment, your heart whom Yi Chan, discovered the beautiful

  • flash.utils package with a variety of package-level functions for timing code execution. retrieve information about classes and objects and convert escaped characters 2010-11-22

    flash.utils package with a variety of package-level functions for timing code execution, retrieving information about classes and objects, and converting escape characters. Public Methods Function Defined clearInterval (id: uint): void Cancels a spec

  • J2SE self (d) - Classes and Objects 2011-01-03

    1, the class definition: class with the same properties and behavior is a set of objects in the collection. 2, the definition of the object: we meet certain criteria to a specific thing called an object of this class, also known as an object instance

  • Js create a table on when the difference between IE and Firefox 2010-05-11

    Js create a table on when the difference between IE and Firefox, when stored in a project, there needs are: point a button to add a table form. Part of the code is as follows (where surveyIdIndex, optionIdIndex, optionPIdIndex a global variable): var

  • J2SE self (e) - Classes and Objects 2011-01-05

    1, the variable is a block of computer memory space 2, Java memory management principle (that the tripartite relationship - process <-> JVM <-> operating system): 1), the program runs before: some programs written in programming languages will

  • Reproduced: Can not create SSIS runtime objects. Verify DTS.dll is available and has been registered. 2011-02-28

    Can not import data when creating SSIS runtime objects. Verify DTS.dll is available and whether he has registered. The wizard can not continue and will terminate. (SQL Server Import and Export Wizard) See details found ===============================

  • JS Create Object 2009-10-19

    javascript object definition (2007-05-23 22:15:46) <script> </ script> tags: javascript Create Object Category: JavaScript Today, the company completed the task in the process, some information poor. This found the following objects on the def

  • 1. To create and destroy objects (1) 2010-05-02

    The series is "Effective Java" the second edition of Starter Edition, hope you like! 1, consider using a static factory method instead of constructor Create object method: First, the most common public constructor, the second is a static factory

  • 2. To create and destroy objects (2) 2010-05-02

    5, to avoid creating unnecessary objects In general, it is best to reuse the object instead of each need the same function when creating a new object. If the object is immutable, it can always be reused. Counter-example: String s = new String ("strin

  • js create dynamic forms. DIV. 2009-10-01

    <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib uri="erptag" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitiona

  • js create the object of several commonly used methods (transfer) 2010-06-24

    The first model: the factory method var lev=function(){ return " Ah play "; }; function Parent(){ var Child = new Object(); Child.name=" Bruce Lee "; Child.age="30"; Child.lev=lev; return Child; }; var x = Parent(); alert(x.n

  • Java Performance Tuning Tips (2) - the use of skills classes and objects 2010-11-05

    1. As little as possible to generate a new object with new With a new instance of the class is created, the constructor chain, all constructors are automatically called, the operation is slow. In some cases can reuse existing objects. String operatio

  • Classes and Objects (JAVA first lesson) 2011-04-21

    Teacher notes 1. What is the target object in real life is real life objects and the existence of objects you can think of. Everything is an object. We live in a world of the middle object. Around an object is any object. Object is composed of two pa

  • js create dynamic div layer 2010-03-29

    <html> <head> <link href="<%=jmfg%>/css/common.css" rel="stylesheet" type="text/css"> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> </head> <body

  • python dynamically create classes 2010-04-03

    python import module is divided into two kinds: 1 import module name Can 2 modulename = 'haha' # module string Then: m = __import__ ('mymodule') The following methods can be used to dynamically create a python class To obtain a property of the module

  • js create element example 2010-07-22

    function showAreaData(weekId){ var currTR = event.srcElement.parentNode; if(hasData==true && lastCell == weekId){ for(var s = 1 ; s <= n ; s ++){ currTR.parentNode.removeChild(currTR.nextSibling); } hasData = false; }else{ if(hasData == true &a

  • js create a dynamic table row to add delete form 2009-06-28

    <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Strict / / EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd "> <html xmlns = " http://www.w3.org/1999/xhtml "> <head> <meta http-equiv="Cont

  • javascript_ _ Return of the King study notes (a) classes and objects 2010-10-04

    The first way var obj = new Object (); obj.a = "2"; obj.b = "3"; obj.change = fucntion () ( alert ('hello'); ) The second approach var obj = (x: 1, y: 2) Third way function test (x, y) ( this.x = x; this.y = y; )