Javascript 继承问题
Javascript Inheritance Woes
我正在努力研究原型继承。
我已经阅读了关于它的 MDN 文档,但它并没有让我变得更聪明。到目前为止我有以下代码...
var Vertex = function(id, options){
this.options = options || {};
this.id = id;
this.position = new THREE.Vector3(0, 0, 0);
this.velocity = new THREE.Vector3(0, 0, 0);
this.acceleration = new THREE.Vector3(0, 0, 0);
this.toString = function(){
return this.id.toString();
};
this.edge_count = 0;
this.edges = {};
if(!this.options.hasOwnProperty('label')){
this.options.label = {
text: '',
direction: 'x',
distance: '10'
};
};
};
... 我想从中 "inherit" 构造函数添加的所有属性。 Vertex 有一个 paint 方法,它添加一个 Mesh 作为 Vertex 的 object
。所以我写了...
var CameraVertex = function(id, camera){
Vertex.call(this);
this.object = camera;
this.id = id;
};
CameraVertex.prototype = Object.create(Vertex.prototype);
CameraVertex.prototype.constructor = CameraVertex;
...所以我可以使用 CameraVertex 作为 Vertex 的替代品(构造函数除外,它只是将相机分配给 Vertex 的对象 属性,它通常包含一个 Mesh或一个组。
但由于某些原因,当我在 CameraVertex 和常规顶点之间创建一条边时,似乎没有 source.object
。
使用 google 单击登录并用鼠标选择顶点后,可以在 Social Cartography 找到完整的示例。
当您调用继承对象的构造函数时,您还需要传递所有必要的参数。
var CameraVertex = function(id, camera, options){
Vertex.call(this, id, options);
this.object = camera;
this.id = id;
};
虽然我不熟悉三,所以我不明白你的问题 source.object,到目前为止我可以看到这个问题。
我正在努力研究原型继承。 我已经阅读了关于它的 MDN 文档,但它并没有让我变得更聪明。到目前为止我有以下代码...
var Vertex = function(id, options){
this.options = options || {};
this.id = id;
this.position = new THREE.Vector3(0, 0, 0);
this.velocity = new THREE.Vector3(0, 0, 0);
this.acceleration = new THREE.Vector3(0, 0, 0);
this.toString = function(){
return this.id.toString();
};
this.edge_count = 0;
this.edges = {};
if(!this.options.hasOwnProperty('label')){
this.options.label = {
text: '',
direction: 'x',
distance: '10'
};
};
};
... 我想从中 "inherit" 构造函数添加的所有属性。 Vertex 有一个 paint 方法,它添加一个 Mesh 作为 Vertex 的 object
。所以我写了...
var CameraVertex = function(id, camera){
Vertex.call(this);
this.object = camera;
this.id = id;
};
CameraVertex.prototype = Object.create(Vertex.prototype);
CameraVertex.prototype.constructor = CameraVertex;
...所以我可以使用 CameraVertex 作为 Vertex 的替代品(构造函数除外,它只是将相机分配给 Vertex 的对象 属性,它通常包含一个 Mesh或一个组。
但由于某些原因,当我在 CameraVertex 和常规顶点之间创建一条边时,似乎没有 source.object
。
使用 google 单击登录并用鼠标选择顶点后,可以在 Social Cartography 找到完整的示例。
当您调用继承对象的构造函数时,您还需要传递所有必要的参数。
var CameraVertex = function(id, camera, options){
Vertex.call(this, id, options);
this.object = camera;
this.id = id;
};
虽然我不熟悉三,所以我不明白你的问题 source.object,到目前为止我可以看到这个问题。