2014-01-17 57 views
5

我有一個關於子原型的問題。Javascript sub prototyping

作爲例子,我想要做的事,如:

var Foo = function() { this.Bar.prototype.Foo = this.Baz.prototype.Foo = this }; 
Foo.prototype.is = "foo"; 
Foo.prototype.Iam = function() { return this.is }; 

Foo.prototype.Bar = function() {}; 
Foo.prototype.Bar.prototype.is = "bar"; 
Foo.prototype.Bar.prototype.Iam = function() { return this.Foo.is + this.is }; 

Foo.prototype.Baz = function() {}; 
Foo.prototype.Baz.prototype.is = "baz"; 
Foo.prototype.Baz.prototype.Iam = function() { return this.Foo.is + this.is }; 

var foo = new Foo(); 
var bar = new foo.Bar(); 
var baz = new foo.Baz(); 

console.log(foo.Iam()); // output: foo 
console.log(bar.Iam()); // output: foobar 
console.log(baz.Iam()); // output: foobaz 

要訪問內酒吧和巴茲foo的對象我this.Bar.prototype.Foo = this.Baz.prototype.Foo = this延長酒吧和巴茲的原型。

我的問題是,如果存在一個更簡單的方法來做到這一點,並訪問Bar和Baz內的主要對象Foo,或者這是通常的方式嗎?我想這樣做的原因是創建一個主對象var x = new Foo()並且所有子對象都可以訪問主對象。

+0

你目前的方法看起來相當不錯 - 每班大約有1條語句將它引入混合。 – goat

+4

這是「子原型」實際上是由一些人實際使用的模式嗎? – basilikum

+1

我沒有看到任何理由將Bar或Baz作爲函數構造函數的原型。我寫下了我的變體,在Foo中加入了Bar和Baz – Andrew

回答

0

我還沒有見過使用太多的子原型。我發現只有這樣:

var Foo = function() {}; 
Foo.prototype.is = "foo"; 
Foo.prototype.Iam = function() { return this.is }; 

Foo.prototype.Bar = function() {}; 
Foo.prototype.Bar.prototype.foo = new Foo(); 
Foo.prototype.Bar.prototype.is = "bar"; 
Foo.prototype.Bar.prototype.Iam = function() { return this.foo.is + this.is }; 

Foo.prototype.Baz = function() {}; 
Foo.prototype.Baz.prototype.foo = new Foo(); 
Foo.prototype.Baz.prototype.is = "baz"; 
Foo.prototype.Baz.prototype.Iam = function() { return this.foo.is + this.is }; 

var foo = new Foo(); 
var bar = new foo.Bar(); 
var baz = new foo.Baz(); 

console.log(foo.Iam()); // output: foo 
console.log(bar.Iam()); // output: foobar 
console.log(baz.Iam()); // output: foobaz 
0
Foo.prototype.Bar = function() {}; 
Foo.prototype.Bar.prototype.foo = new Foo(); 

是的,這正是我發現大部分時間,但如果我走這條路,然後變爲美孚()都將丟失。

在我上面的例子中,我可以設置「全局」參數爲Foo(),它們可以從Bar()和Baz()訪問。例如:

var extend = function(a, b) { 
    var n; 
    for (n in b) { 
     a[n] = b[n]; 
    } 
}; 

var Foo = function(o) { 
    extend(this, o); 
    this.Bar.prototype.Foo = this.Baz.prototype.Foo = this; 
}; 

Foo.prototype.is = "foo"; 
Foo.prototype.Iam = function() { return this.is }; 
Foo.prototype.greeting = "hello, I am"; 

Foo.prototype.Bar = function() {}; 
Foo.prototype.Bar.prototype.is = "bar"; 
Foo.prototype.Bar.prototype.Iam = function() { return this.Foo.greeting +" "+ this.Foo.is + this.is }; 

Foo.prototype.Baz = function() {}; 
Foo.prototype.Baz.prototype.is = "baz"; 
Foo.prototype.Baz.prototype.Iam = function() { return this.Foo.is + this.is }; 

var foo = new Foo({ greeting: "hi my friend, I am" }); 
var bar = new foo.Bar(); 
console.log(bar.Iam()); 

//output is: hi my friend, I am foobar 

我將問候語設置爲可從所有子功能訪問的Foo()。這樣做的原因是,對象存在不同的「全局」參數。如果在Foo()上更改了「全局」參數,那麼它應該會影響Bar()和Baz()。

我真的不知道這是否是正確的方式,因爲我剛開始學習Javascript。我接受新的想法。

問候, 強尼

0

也許這裏是你在找什麼:

var extend = function(a, b) { 
    var n; 
    for (n in b) { 
     a[n] = b[n]; 
    } 
}; 

var Foo = function(o) { 
    extend(this, o); 
    var foo = this; 

    this.Bar = function() {}; 
    this.Bar.prototype.is = "bar"; 
    this.Bar.prototype.Iam = function() { return foo.is + this.is }; 

    this.Baz = function() {}; 
    this.Baz.prototype.is = "baz"; 
    this.Baz.prototype.Iam = function() { return foo.is + this.is }; 
}; 
Foo.prototype.is = "foo"; 
Foo.prototype.Iam = function() { return this.is }; 

var foo = new Foo({ is: "foo2" }); 
var bar = new foo.Bar(); 
var baz = new foo.Baz(); 

console.log(foo.Iam()); // output: foo2 
console.log(bar.Iam()); // output: foo2bar 
console.log(baz.Iam()); // output: foo2baz 
0

有美孚只有一個實例,所以你可以聲明FOO作爲對象常量:

var foo = { 
    is:"foo", 
    Iam:function(){return this.is} 
}; 

foo.Bar = function() {}; 
foo.Bar.prototype.is = "bar"; 

foo.Baz = function() {}; 
foo.Baz.prototype.is = "baz"; 

foo.Bar.prototype.Iam = foo.Baz.prototype.Iam 
    = function() { return foo.is + this.is }; 

var bar = new foo.Bar(); 
var baz = new foo.Baz(); 

console.log(foo.Iam()); // output: foo 
console.log(bar.Iam()); // output: foobar 
console.log(baz.Iam()); // output: foobaz