Implementation of two inheritance mechanisms of JScript
Source: Internet
Author: User
The inheritance of JScript can be implemented in two ways: the prototype attribute of the object and the call function. The first method also applies to Javascript, and the second method only applies to JScript. The following example shows the inheritance implemented using prototype: var person = function (){
This. Name = "person ";
} VaR _ p = person. Prototype; _ p. getname = function (){
Return this. Name;
}
_ P. setname = function (name ){
This. Name = Name;
} Var Jeffrey = function (){
This. Name = "Jeffrey"; this. Say = function (Word ){
Alert (Word );
}
} _ P = Jeffrey. Prototype = new person (); // implement inheritance
_ P. Say = function (Word ){
Alert (Word );
} Code Tested: var Jeffrey = new Jeffrey (); alert (Jeffrey. getname (); // call the parent object definition method Jeffrey. setname ("Jeffrey he"); // call the method alert (Jeffrey. getname (); Jeffrey. say ("hello"); then let's look at the example of using the call function: function person () {This. name = "person"; this. getname = function () {return this. name;} This. setname = function (name) {This. name = Name ;}} function Jeffrey () {person. call (this); // implement the inheritance of this. name = "Jeffrey"; this. say = Fu Nction (Word) {alert (Word) ;}} the test code is the same as above.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.