デカルト座標ライブラリで作業しています。クラスを簡単に作成し、プロトタイプを定義してインスタンスを作成しようとします。新しいプロトタイプ定義なしで作成されたオブジェクト
しかし、この単純なケースでは、私のデカルトオブジェクトにはメソッドがありません。
let Cartesian = function(instance, opts) {
let pos = opts.pos || {x:0, y: 0}
/* .. Logic here .. */
return {pos: pos}
}
console.log("Before : ", Cartesian.prototype.getPosition)
Cartesian.prototype.getPosition = function() {
return true
}
console.log("After : ", Cartesian.prototype.getPosition)
CartesianExport = {
create: function (instance, opts) {
console.log("Create new : ", Cartesian.prototype.getPosition)
let c = new Cartesian(instance, opts)
console.log("Created : ", c, c.getPosition)
return c
}
}
module.exports = CartesianExport
そして、これは私のコンソール
Before : undefined
After : function() { return true }
Create new : function() { return true }
Created : Object {pos: Object} undefined
である私はそれを使用しようとすると、私はcartesian.getPosition is not a function
を得た理由を私は理解していません。
'Cartesian'では 'return {pos:pos}' - なぜそれをやっていますか?私はあなたがJavascriptのコンストラクタがどのように動作するのか誤解していると思います。 – user2357112
私の場合、私は他の属性を返します。その場合、 'c.pos' – Arthur
からアクセスできます。返されるオブジェクトは'デカルト 'ではありません –