JSでオブジェクトを作っています。
生成時に引数を渡すのですが、そこからある程度複雑な処理をして、別のプロパティを算出したいです。
簡略にすると以下のようなものです。
var Name=function(first,last){ this.first=first; this.last=last; this.full=""; this.compute=function(){ this.full = this.first + this.last; //実際はかなり複雑な処理。 } }; N=new Name("taro","yamada"); N.compute(); console.log(N.full);
ここでthis.full
を得ようと思うと、N.compute()
を実行しなければならないと思います。
そうではなく、N=new Name("taro","yamada")
の時点で、自動でcompute
にあたる処理も実行してしまいたいです。
自分なりに調べてみたものの、よくわかりませんでした。
どのようにしたらよいのでしょうか。
なお、pythonでいえば以下のイメージになります。
__init__
で self._compute()
を呼び出せていますが、そういう処理をしたいです。
class Name(): def __init__(self,first,last): self.first=first self.last=last self._compute() def _compute(self): self.full=self.first + self.last
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。