Javascript --- kind of inheritance
1. The first approach, posing as the way the object. (Using js in the name of each method is a Function object)
Java code
- / / The first approach, posing as the way the object. (Using js in the name of each method is a Function object)
- function Parent (username) (
- this. username = username;
- this. say = function () (
- alert (this. username);
- )
- )
- function Child (username, password) (
- 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.
- this. temp (username); / / initialization method where the content of
- delete this. temp; / / temp is not used. Can be removed. This can not be lost
- / / 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
- 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
// 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
- / / call () function is a function object Function
- / / Specific use, such as
- function test (str) (
- alert (this. username + "," + str);
- )
- var o = new Object ();
- o.username = "zhangsan";
- 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
- 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
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
- / / Third ways: apply () function of the way, apply () and call () is the same, only different parameter, apply for an array of parameters
- / / Therefore, the realization of the succession can be
- 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
// 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
- / / The prototype chain of succession
- 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 ();
// 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
- / / Mixed-mode of succession
- 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 ();
// 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();
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