我做在JavaScript中使用類和原型模式的不確定。我做了一個名爲NavigatieButton
功能,並獲得命名location
,rotation
和navigatie
三個屬性的構造函數。使用類原型模式
在NavigatieButton
原型,我maked一個名爲init
功能必須呈現一個「組件」。
裏面的render
功能我做的NavigatieButton
一個新實例使用下面的代碼:
var navBtn = new NavigatieButton('0 0 0');
但如果我叫init
,性能location
,rotation
和navigatie
是不確定的。
在這裏,你可以找到我的代碼我做了。
"use strict";
function NavigatieButton(location) {
this.location = location;
}
NavigatieButton.prototype.init = function() {
var element = document.createElement('a-entity');
element.setAttribute('location', this.location);
// Here I'm adding some other nodes to the `element` variable.
return element;
};
(function() {
function render() {
var el = document.createElement('div');
for (var i = 7; i--;) {
var navBtn = new NavigatieButton('0 0 0');
var comp = NavigatieButton.prototype.init();
el.appendChild(comp);
}
console.log(el);
}
render();
})();
打開瀏覽器查看記錄數據的控制檯。在下面你可以找到記錄數據的一個片段。
<div>
<!-- 7 thimes this code: -->
<a-entity location="undefined" rotation="undefined">
<a-image src="./assets/images/navigationOuterCircle.png"></a-image>
<a-image src="./assets/images/navigationInnerCircle.png" scale="0.5 0.5 0.5"></a-image>
</a-entity>
<!-- Till here -->
</div>
我的代碼出了什麼問題?爲什麼我一直沒有定義,而不是我的輸入?
PS 1:我下面這篇文章:3 ways to define a JavaScript class(見標題1.2)。
PS 2:我使用aframe來創建WebVR。