gpt4 book ai didi

javascript - 我如何在子 javascript 代码中使用父变量?

转载 作者:行者123 更新时间:2023-11-29 20:02:07 27 4
gpt4 key购买 nike

此代码在子级中使用继承 我在场景中添加了一些在父级中声明的东西 我该怎么做 它在子级中看到场景时出错

 function parent    (domElement, renderStatistics) {
this.scene = new THREE.Scene();
}
function child(domElement) {
parent.call(this, domElement);
this.init();
}
child.prototype = Object.create(parent.prototype);

child.prototype.constructor = Young;


child.prototype.init = function () {
function createLab(geometry) {
var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
this.scene.add(mesh); // this error Cannot call method 'add' of undefined
}
}

最佳答案

child.prototype.init = function () {
var _this = this;
function createLab(geometry) {
var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
_this.scene.add(mesh);
}
}

关于javascript - 我如何在子 javascript 代码中使用父变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13810458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com