Javascript --- kind of inheritance

This article is reproduced in: http://ttitfly.javaeye.com/blog/194252

1. The first approach, posing as the way the object. (Using js in the name of each method is a Function object)

Java code

  1. / / The first approach, posing as the way the object. (Using js in the name of each method is a Function object)
  2. function Parent (username) (
  3. this. username = username;
  4. this. say = function () (
  5. alert (this. username);
  6. )
  7. )
  8. function Child (username, password) (
  9. this. temp = Parent; / / temp to the point where Parent. Js in the use of each method name is a Function object, point to a method.
  10. this. temp (username); / / initialization method where the content of
  11. delete this. temp; / / temp is not used. Can be removed. This can not be lost
  12. / / Parent (username); / / write the surface this appears to be correct, in fact, is wrong. Because only the new object is out of this, so call the Parent in the value of this would be no
  13. this. password = password;
  14. this. hello = function () (
  15. alert (this. password);
  16. )
  17. )
  18. var parent = new Parent ( "zhangsan");
  19. parent.say ();// zhangsan
  20. var child = new Child ( "lisi", "123456");
  21. child.say ();// lisi
  22. child.hello ();// 123456

// The first way  , Fake objects  .( Use js in each method name is a function object  )
function Parent(username){
	this.username = username;
	this.say = function(){
		alert(this.username);
	}
}

function Child(username,password){
	
	this.temp = Parent;//temp  Point to the place pointed to by the Parent. Use js in each method name is a function object  , Point to a method.  
	this.temp(username);// The contents of the initialization method  
	delete this.temp;//temp No use. You can directly deleted  .this Not lost  
	
	
	//Parent(username);// Write on the surface appear to be correct, is wrong. Because only the new out of the object will have this  , So call in this respect there is no value for the  
	
	
	this.password = password;
	this.hello = function(){
		alert(this.password);
	}
}

var parent = new Parent("zhangsan");
parent.say();//zhangsan

var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456


2. The second way: call () function the way

call () function is a function of the Function object, the specific use of the following

Java code

  1. / / call () function is a function object Function
  2. / / Specific use, such as
  3. function test (str) (
  4. alert (this. username + "," + str);
  5. )
  6. var o = new Object ();
  7. o.username = "zhangsan";
  8. test.call (o, "123456 ");// zhangsan, 123456. because everyone has a Function Object call () method, and the function name is a Function object. call () function's first parameter is the test function in of this.

//call() A function is a function object in a function  
// Specific usage such as  
function test(str){
	alert(this.username + ","  + str);
}

var o = new Object();
o.username = "zhangsan";
test.call(o,"123456");//zhangsan,123456 . Because every Function object has a call  () The method and the name of the function is a function object  .call() The function of the first parameter is the test function in this  .


Therefore, the succession can be achieved by:

Java code

  1. function Parent (username) (
  2. this. username = username;
  3. this. say = function () (
  4. alert (this. username);
  5. )
  6. )
  7. function Child (username, password) (
  8. Parent.call (this, username);
  9. this. password = password;
  10. this. hello = function () (
  11. alert (this. password);
  12. )
  13. )
  14. var parent = new Parent ( "zhangsan");
  15. parent.say ();// zhangsan
  16. var child = new Child ( "lisi", "123456");
  17. child.say ();// lisi
  18. child.hello ();// 123456

function Parent(username){
	this.username = username;
	this.say = function(){
		alert(this.username);
	}
}
function Child(username,password){
	Parent.call(this,username);
	this.password = password;
	this.hello = function(){
		alert(this.password);
	}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan

var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456


3. The third way to achieve: apply () function of the way, apply () and call () is the same, only different parameter, apply for an array of parameters

Java code

  1. / / Third ways: apply () function of the way, apply () and call () is the same, only different parameter, apply for an array of parameters
  2. / / Therefore, the realization of the succession can be
  3. function Parent (username) (
  4. this. username = username;
  5. this. say = function () (
  6. alert (this. username);
  7. )
  8. )
  9. function Child (username, password) (
  10. Parent.apply (this, new Array (username));
  11. this. password = password;
  12. this. hello = function () (
  13. alert (this. password);
  14. )
  15. )
  16. var parent = new Parent ( "zhangsan");
  17. parent.say ();// zhangsan
  18. var child = new Child ( "lisi", "123456");
  19. child.say ();// lisi
  20. child.hello ();// 123456

// The third approach  :apply() How to apply the function  () And call  () Is the same, but different arguments passed  ,apply The parameter is an array  
// So this can be inherited  

function Parent(username){
	this.username = username;
	this.say = function(){
		alert(this.username);
	}
}
function Child(username,password){
	Parent.apply(this,new Array(username));
	this.password = password;
	this.hello = function(){
		alert(this.password);
	}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan

var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456


4. The first four kinds of ways: the prototype chain of succession

Java code

  1. / / The prototype chain of succession
  2. function Parent () (
  3. )
  4. Parent.prototype.hello = "hello";
  5. Parent.prototype.sayHello = function () (
  6. alert (this. hello);
  7. )
  8. function Child () (
  9. )
  10. Child.prototype = new Parent ();// key
  11. Child.prototype.world = "world";
  12. Child.prototype.sayWorld = function () (
  13. alert (this. world);
  14. )
  15. var child = new Child ();
  16. child.sayHello ();
  17. child.sayWorld ();

// The prototype chain implementation inheritance  
function Parent(){
}
Parent.prototype.hello = "hello";
Parent.prototype.sayHello = function(){
	alert(this.hello);
}

function Child (){
}
Child.prototype = new Parent();// Key  

Child.prototype.world = "world";
Child.prototype.sayWorld = function(){
	alert(this.world);
}

var child = new Child();
child.sayHello();
child.sayWorld();


5. Fifth ways: mixed mode of succession

Java code

  1. / / Mixed-mode of succession
  2. function Parent (hello) (
  3. this. hello = hello;
  4. )
  5. Parent.prototype.sayHello = function () (
  6. alert (this. hello);
  7. )
  8. function Child (hello, world) (
  9. this. world = world;
  10. Parent.call (this, hello); / / key
  11. )
  12. Child.prototype = new Parent (); / / key
  13. Child.prototype.sayWorld = function () (
  14. alert (this. world);
  15. )
  16. var child = new Child ( "hello", "world");
  17. child.sayHello ();
  18. child.sayWorld ();

// Mixed-mode implementation inheritance  
function Parent(hello){
	this.hello = hello;
}
Parent.prototype.sayHello = function(){
	alert(this.hello);
}

function Child(hello,world){
	this.world = world;
	Parent.call(this,hello);// Key  

}
Child.prototype = new Parent(); // Key  
Child.prototype.sayWorld = function(){
	alert(this.world);
}

var child = new Child("hello","world");
child.sayHello();
child.sayWorld();
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Javascript --- kind of inheritance

  • JavaScript inheritance

    About JavaScript inherited a small example ... <! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 / / EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" conte ...

  • JavaScript String Functions Guinness

    Own JS function 1.Asc (x), Chr (x): convert characters, character code 2. Filter: Search string array of a specific string Format: v = filter (x, s [, include [, compare]]) Examples: Dim x ()={" kjwang "," wangkj "," peter &q ...

  • EJB ant script to deploy template works

    <? xml version = "1.0" encoding = "UTF-8"?> <! - Name Project name basedir build.xml file directory -> <project name="HelloWorld" basedir="."> <! - Property variables -> <! - The sour ...

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • java read file

    java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to und ...

  • FLEX: integrating Spring + Hibernate

    Before a friend also wanted to study development of FLEX. Asked me to help him to be a small sample. Spent a weekend time, to integrate a sampleproject. Client: FLEX Server: Spring2.5 + Hibernate3.2 + Hibernate-annotations3.3.1 + MySQL5 FDS: BlazeDS3 ...

  • JAVA Class naming convention

    1. Entity Layer: Inheritance: All categories inherited from BasicEntity, one of BasicEntity implementation java.io.Serializable interface; Naming rules: Class Name = Object + type suffix, one of type suffix for Bean, such as: SalesOrderBean 2. Form l ...

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

  • Great collection of java interview topics

    1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract d ...

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries