可以在Javascript中创建属性吗?

时间:2014-08-04 18:02:36

标签: javascript html

如果我在JavaScript中有一个以var stars = []开头的数组,我创建了一个star(下面的代码)。我在网上发现了这个代码,并且正在通过它,以便我可以看到它是如何工作的并进行修改。

this.stars = stars;是否只为此特定类创建了另一个内部属性?

var stars = []; 
for(var i=0; i<this.stars; i++) {
    stars[i] = new Star(Math.random() * this.width, 
                        Math.random() * this.height, 
                        Math.random() * 3+1,
                       (Math.random() * (this.maxVelocity - this.minVelocity)) 
                        + this.minVelocity);
}

this.stars = stars;  // <-- creating internal property

因为我在这个类的定义中没有看到它。所以我不确定它是否只是在现场创建,或者是否可以在此定义中声明。


代码在这里:

function Starfield() {
    this.fps    = 30;
    this.canvas = null;
    this.width  = 0;
    this.width  = 0;
    this.minVelocity = 15;
    this.maxVelocity = 30;
    this.stars = 9000;
    this.intervalId = 0;
}

//  The main function - initialises the starfield.
Starfield.prototype.initialise = function(div) {
    var self = this; //sets it self to current object

    //  Store the div
    this.containerDiv = div;
    self.width        = window.innerWidth;
    self.height       = window.innerHeight;

    window.onresize = function(event) {
        self.width  = window.innerWidth;
        self.height = window.innerHeight;
        self.canvas.width  = self.width;
        self.canvas.height = self.height;
        self.draw();
    }

    //  Create the canvas.
    var canvas = document.createElement('canvas');
    div.appendChild(canvas);
    this.canvas = canvas;
    this.canvas.width  = this.width;
    this.canvas.height = this.height;
};

Starfield.prototype.start = function() {
    //  Create the stars.
    var stars = []; //creates an array that can be used for anything but in this case a star field

    //this.stars is a property in the class that contains a number of the stars
    for(var i=0; i<this.stars; i++) {
        stars[i] = new Star(Math.random() * this.width, 
                            Math.random() * this.height, 
                            Math.random() * 3+1,
                           (Math.random() * (this.maxVelocity - this.minVelocity)) + this.minVelocity);
    }

    this.stars = stars;

    var self = this;
    //  Start the timer.
    this.intervalId = setInterval(function() {
        self.update();
        self.draw();    
    }, 1000 / this.fps);
};

Starfield.prototype.stop = function() {
    clearInterval(this.intervalId);
};

Starfield.prototype.update = function() {
    var dt = 1 / this.fps;

    for(var i=0; i < this.stars.length; i++) {
        var star = this.stars[i];
        star.y += dt * star.velocity;
        //  If the star has moved from the bottom of the screen, spawn it at the top.
        if (star.y > this.height) {
            this.stars[i] = new Star(Math.random() * this.width, 
                                     0, 
                                     Math.random() * 3 + 1, 
                                     (Math.random() * (this.maxVelocity + 60 - this.minVelocity)) + this.minVelocity);
        }
    }
};

Starfield.prototype.draw = function() {
    var ctx = this.canvas.getContext("2d");

    //  Draw the background.
    ctx.fillStyle = '#000000';
    ctx.fillRect(0, 0, this.width, this.height);

    //  Draw stars.
    ctx.fillStyle = '#ffffff';
    for(var i=0; i<this.stars.length;i++) {
        var star = this.stars[i];
        ctx.fillRect(star.x, star.y, star.size, star.size);
    }
};

//This is the class for stars -- there are 4 properties that are in this particular class
function Star(x, y, size, velocity) {
    this.x = x;
    this.y = y; 
    this.size = size;
    this.velocity = velocity;
}

2 个答案:

答案 0 :(得分:2)

是的,可以随时在JavaScript中添加属性。看一下这个例子:

function Person(firstName) {
    this.firstName = firstName;
}

Person.prototype.getFullName = function() {
    // Notice that I haven't defined this.lastName yet
    return this.firstName + ' ' + this.lastName;
};

var bob = new Person('Bob');
bob.lastName = 'Saget';
console.log(bob.getFullName()); // 'Bob Saget'

答案 1 :(得分:1)

是的,Javascript对象是动态的。他们可以随时添加/删除新属性,除非他们已sealed,并且可以随时修改其属性,除非他们已frozen

你可能不会在野外看到很多密封或冻结的物体。