JS:功能未定义

时间:2018-02-24 08:46:38

标签: javascript function oop defined

我一直在尝试制造一些星空。这是代码。 当我尝试加载页面时,控制台会说createHtmlElementFromStar未定义。我找不到原因。 谢谢你的答案!

 var Star = function (x, y, look, color) {
    var _x = x;
    var _y = y;
    var _look = look;
    var _color = color;

    this.createHtmlElementFromStar = function () {
        var st = document.createElement("span");
        st.style.position = "absolute";
        st.style.left = x + "px";
        st.style.top = (y - 10) + "px";
        return st;
    };

};

var Stars = function () {
    var _stars = new Array();

    this.createStars = function (look, count) {
        var x = Math.random() * (400 - x) + x;
        var y = Math.random() * (400 - y) + y;
        var look = "";
        var color = "#"+((1<<24)*Math.random()|0).toString(16);
        _stars.push(new Star(x, y, look, color));
    };

    this.showStars = function () {
        for (var i = 0; i < _stars.length; i++) {
        document.body.appendChild(createHtmlElementFromStar(_stars[i]));
        };
    };};


var stars = new Stars();
stars.createStars(1, 50, "*", 150);
stars.showStars();

0 个答案:

没有答案