每個function有prototype屬性,稱為原型。每個物件也有個原型,Firefox/Safari/Chrome/Opera 中可以透過__proto__來訪問,IE6/7/8中沒有提供相關介面。
function Person(){
function Person(){
function Person(){
this function(){}
}
Person.prototype.method2 = function(){}
function Man(){}
Man.prototype = new Person();
Man.prototype.m1 = function(){}
Man.prototype.m2 = function(){}
var m = new Man(); for(var a in m.__proto__){
alert(a);
}
複製程式碼
程式碼如下:
function Person(){
function Person(){
function Person(){
this function(){alert(1)}
}
Person.prototype.method2 = function(){alert(2);}
function Man(){
this.m1 = this.m1 = function(){
Object.getPrototypeOf(this).method1();
}
}
Man.prototype = new Person(); prototype.m2 = function(){ Object.getPrototypeOf(this).method2();
}
var man = new Man()
Java中是這樣的
複製程式碼
程式碼如下:
package b1; 🎜>
class Person {
private String name;
Person(String name) {
1() {
System.out.println(this.name);
}
}
class Man extends Person{
🎜> }
public void m1() {
super.method1(); Man man1 = new Man("Jack");
man1.m1();
}
}