(Acknowledgment: This way of introducing JavaScript is inspired by the methods of Seb Lee-Delisle.)
particles.jslet canvas;
let context;
let width;
let height;
let x = 250;
let y = 150;
let size = 10;
let xChange = getRandomNumber(-10, 10);
let yChange = getRandomNumber(-10, 10);
document.addEventListener('DOMContentLoaded', init, false);
function init() {
canvas = document.querySelector('canvas');
context = canvas.getContext('2d');
width = canvas.width;
height = canvas.height;
window.setInterval(draw, 33);
}
function draw() {
context.clearRect(0, 0, width, height);
context.fillStyle = 'yellow';
context.fillRect(x, y, size, size);
x = x + xChange;
y = y + yChange;
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
| Python | JavaScript |
|---|---|
if x < y:
print('x is smaller than y')
elif x == y:
print('x is equal to y')
else:
print('x is larger than y')
|
if (x < y) {
console.log('x is smaller than y')
} else if (x === y) {
console.log('x is equal to y')
} else {
console.log('x is larger than y')
}
|
In the JavaScript, note:
Also, where Python uses and, or
and not, JavaScript uses &&,
|| and !
draw()function draw() {
context.clearRect(0, 0, width, height);
context.fillStyle = 'yellow';
context.fillRect(x, y, size, size);
x = x + xChange;
y = y + yChange;
if ( x < 0 ) {
xChange = xChange * - 1;
} else if ( x + size > width ) {
xChange = xChange * - 1;
}
if ( y < 0 ) {
yChange = yChange * - 1;
} else if ( y + size > height ) {
yChange = yChange * - 1;
}
}
Python is a strongly-typed language, whereas JavaScript is weakly-typed
| Python (they all produce error messages) | JavaScript (they all do type coercions) |
|---|---|
x = 'abc' + 12 |
x = 'abc' + 12; |
x = 'abc' - 12 |
x = 'abc' - 12; |
if 'abc' == 3:
#do something
else:
#do something else
|
if ('abc' == 3) {
# do something
} else {
# do something else
} |
if '3' == 3:
#do something
else:
#do something else
|
if ('3' == 3) {
# do something
} else {
# do something else
} |
===), instead of equality (==)
let twinA = {
firstname : 'John',
surname : 'Grimes',
age : 25
}
let twinB = {
firstname : "Edward",
surname : "Grimes",
age : 25
}
twinA.firstname
The 'equivalent' of a Python list is a JavaScript array
| Python | JavaScript |
|---|---|
groceries = ['eggs', 'milk', 'tea'] |
let groceries = ['eggs', 'milk', 'tea']; |
len(groceries) |
groceries.length |
groceries.append('bread')
|
groceries.push('bread')
|
(JavaScript arrays are very similar to Python lists, but not so similar to arrays in languages such as C or Java)
for loops| Python | JavaScript |
|---|---|
for item in groceries:
print(item)
|
for (let item of groceries) {
console.log(item);
}
|
| Python | JavaScript |
|---|---|
for i in range(10):
print(i)
|
for (let i = 0; i < 10; i += 1) {
console.log(i);
}
|
particles.jslet canvas;
let context;
let width;
let height;
let ps = [];
let gravity = 0.5;
document.addEventListener('DOMContentLoaded', init, false);
function init() {
canvas = document.querySelector('canvas');
context = canvas.getContext('2d');
width = canvas.width;
height = canvas.height;
window.setInterval(draw, 33);
}
function draw() {
for (let i = 0; i < 30; i += 1) {
let p = {
x : 250,
y : 150,
size : 10,
xChange : getRandomNumber(-10, 10),
yChange : getRandomNumber(-10, 10)
};
ps.push(p)
}
context.clearRect(0, 0, width, height);
context.fillStyle = 'yellow';
for (let p of ps) {
context.fillRect(p.x, p.y, p.size, p.size);
p.x = p.x + p.xChange;
p.y = p.y + p.yChange;
p.yChange = p.yChange + gravity;
}
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}