可以在任何地方用Javascript创建属性吗?

Can properties be created in Javascript any where?

本文关键字:创建 属性 Javascript 方用 任何地      更新时间:2023-09-26

如果我在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;
}

是的,属性可以在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'

是的,Javascript对象是动态的。它们可以随时添加或删除新的属性,除非它们已被密封;它们的属性可以随时修改,除非它们已被冻结。

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