2012-03-06 15 views
1

ねえ、私は質問があります。私は小さなJsオブジェクトを書いて、ページあたりの適切なスクリプト/スタイルをロードできるようにするために、私がどのページを管理しているかを簡単に管理します。私は理解していない状況にぶつかっています。私は、明らかに十分な現在のページに設定されるプロパティcurrentPageを持っていますが、私は以前に定義した別のプロパティからまっすぐに設定した場合、参照エラーを返しますが、同じ事を返す関数働く私はそれがなぜであるかわからない。誰かが私にこれを説明することはできますか?私はハードコアのJS開発者ではありません。私は行くにつれて物事を理解するので、これはJS固有のものですか?ここで私が何を意味するかのコードサンプルです:Javascript OOPヘルプ/アドバイス/説明

var self = PageInfo = { 
    locationArray : window.location.pathname.toString().split("/"), 

    printOutPath : function(){ 
     console.log(self.locationArray.length); 
    }, 
    //ref. error to locationArray 
    parentDirectory : self.locationArray[self.locationArray.length -3], 
    currentPage : function() { 
     return self.locationArray[self.locationArray.length -2]; // works 
    } 
}; 
+0

するvar自己を設定する機能を作成することができますPageInfo = {}?おそらくあなたはPageInfoのコンテキストの中でこれを使用したいでしょうか? – thescientist

+0

、それは1行目に 'self'を定義しています。また、 'PageInfoObj.parentDirectory'や' PageInfoObj.parentDirectory() '(呼び出しや参照)を使用していますか? –

+0

私はどこにでも私を得ることができるかどうかを見るためにちょうど追加されました。それは本当にしませんでした。 var PageInfo = {}は私がそれを進める方法です。 – Kobby

答えて

4

JavaScriptを使用する場合(中括弧{}を持つオブジェクトを作成する)リテラル構文をオブジェクトの各プロパティに行くの値は、オブジェクトの時点で評価されます式です創造された。オブジェクトはまだ存在しないため、同じオブジェクトのプロパティを参照することはできません。

self変数を作成する代わりに、オブジェクトのメソッド内でthisを使用できることに注意してください。

var PageInfo = { 
    locationArray : window.location.pathname.toString().split("/"), 

    printOutPath : function(){ 
     console.log(this.locationArray.length); 
    }, 

    currentPage : function() { return this.locationArray[this.locationArray.length -2];} 
}; 

alert(PageInfo.currentPage()); 

さらに読書:https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects

限り、あなたはこのように、ドット構文を使用してメソッドを呼び出すよう:あなたがこれを行うことができますので

PageInfo.currentPage() 

...方法this内で自動的にオブジェクトを参照します

+0

チップをありがとう。私はそれがそれに沿ったものだと思った。私が次のような行に沿って移動した方が良いでしょうか? 'function PageInfo(){ this.locationArray = window.location.pathname.toString()。split("/")... } このようにオブジェクトを作成しましたか? – Kobby

+0

「より良い」という意味に依存します。そうするならば、プロパティは一度に1つずつ作成されるので、すでに作成されたものを参照できますが、 'var something = new PageInfo();'と言う必要があります。複数のインスタンスを作成する必要がある場合は、コンストラクター関数が当てはまります。別のオプションは、オブジェクトリテラルに大部分のプロパティを作成し、その後に 'self.parentDirectory = self.locationArray [self.locationArray.length -3];で' parentDirectory'を追加することです。 '。 – nnnnnn

0

オブジェクトを定義するときには、そのオブジェクトが作成されるまで参照することはできません。関数を使用することで、オブジェクトが作成されるまでself.locationArrayの参照が遅延します。

0

オブジェクトは、文の実行後にのみselfPageInfoに割り当てられます。 ステートメントの後にそれをしてください。

var self = PageInfo = { 
    locationArray : window.location.pathname.toString().split("/"), 

    printOutPath : function(){ 
     console.log(self.locationArray.length); 
    }, 

    currentPage : function() { return self.locationArray[self.locationArray.length -2]; // works 
    } 
}; 

self.parentDirectory = self.locationArray[self.locationArray.length -3]; 

それはそれはより多くのOO

var self = PageInfo = { 
    locationArray : window.location.pathname.toString().split("/"), 

    printOutPath : function(){ 
     console.log(this.locationArray.length); 
    }, 

    currentPage : function() { return this.locationArray[this.locationArray.length -2]; // works 
    } 
}; 

self.parentDirectory = self.locationArray[self.locationArray.length -3];  

作るPageInfo

使用this内部の機能を更新する必要も= parentDirectory

var self = PageInfo = { 
    locationArray : window.location.pathname.toString().split("/"), 

    printOutPath : function(){ 
     console.log(this.locationArray.length); 
    }, 

    parentDirectory:"", 

    setParentDirectory: function() { 
     this.parentDirectory = this.locationArray[this.locationArray.length -3]; 
    }, 

    currentPage : function() { return this.locationArray[this.locationArray.length -2]; } 

}; 

self.setParentDirectory();