可以在任何地方用Javascript创建属性吗?
Can properties be created in Javascript any where?
如果我在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对象是动态的。它们可以随时添加或删除新的属性,除非它们已被密封;它们的属性可以随时修改,除非它们已被冻结。
你可能不会在野外看到很多密封或冰冻的物体。
相关文章:
- 创建属性值数组,其中属性名称以特定字符串结尾
- Do let语句在全局对象上创建属性
- 如何在 javascript 中为策略设计模式创建属性
- 如何在 JavaScript 中基于另一个对象动态创建属性和对象
- 在运行时为对象创建属性
- Javascript对象:动态创建属性和属性名称
- Javascript对象属性-如果不存在,则创建属性
- 如何在使用纯js创建属性时执行某些操作
- 自动创建属性不起作用
- TypeError:无法创建属性'mapTypeId'在字符串'无法加载映射'流星错误
- 可以在任何地方用Javascript创建属性吗?
- 什么时候在当前对象和原型上创建属性?
- 在文字对象中创建属性
- 使用JSON从对象数组创建属性数组
- 如何创建属性
- 如何使用JavaScript在SVG中创建属性
- 当ngAnnotate说“不能创建属性'$methodName'..”是什么意思?
- 无法创建属性'选择'在字符串'信息技术'angularjs
- 在ng模型中动态创建属性
- 如何在ng重复作用域内创建属性