That's it! Most things are objects.
var a = "9";
console.log(a + "1"); // prints "91"
console.log(a + 1); // prints ??
console.log(a - 1); // prints ??
var b = "10";
console.log(1 + +b); // prints 11
console.log(1++b); // prints ??
console.log([] + []); // prints ""
console.log(+["1"]/2 + (+[]) - []*2); // I quit
var a = 1;
a = "foo";
var b = function() {}
function c(arg) {
console.log(arg);
}
var a = 3.14,
b = "hello",
c = false,
d = [1, 2, 3],
e = {foo: "bar"};
console.log(e.foo, e["foo"]);
e.foo = "hello, world";
e[1] = true;
if (true) {
var a = true;
}
console.log(a); // prints true
console.log(foo); // prints undefined (but not error) var foo = "bar"; (function(){ console.log(foo); // prints undefined (not "bar")var foo; })();
function run5(f){ f(5); }
run5(function(n){
console.log(n + 1);
}); // prints 6
function adder(increment){
var num = 0;
return function(){
num += increment;
return num;
}
}
var add2 = adder(2);
console.log(add2()); // prints 2
console.log(add2()); // prints 4
function logger() {
console.log(arguments.length);
console.log.apply(console, arguments);
}
logger(1, "2", [3]); // prints 3 and then the three values
function increment(i, debug){
if (debug) {
console.log("increment called on " + i);
}
return i + 1;
}
console.log(increment(0)); // just prints 1
console.log(increment(1, true)) // prints "increment called on 1" then 2
var a = [1, 2, 3];
if (a[0] == 1){
// something
} else {
// something else
}
for (var i = 0; i < a.length; i++) {
console.log(a[i]);
}
while(true) {
a[0]++;
}
switch, try/catch, and for...in
function Animal() {}
var Will = new Animal();
var James = new Animal();
function Animal(name) {
this.name = name;
}
var Will = new Animal('Will');
console.log(Will.name); // prints "Will"
function Animal(name) {
this.name = name;
}
Animal.prototype.talk = function() {
console.log('My name is ' + this.name);
}
var Will = new Animal('Will');
Will.talk(); // prints "My name is Will"
var func = Will.talk;
func(); // prints "My name is"
func.call({name: 'Slim Shady'}); // prints "My name is Slim Shady"
"The the class-less programming style has grown increasingly popular, and has been adopted for programming languages such as JavaScript, Cecil, NewtonScript, Io, MOO, REBOL, Kevo, Squeak and several others."
function Animal(name){ ... }
Animal.prototype = { ... }
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.woof = function() {
console.log('MOO');
this.talk();
}
var Will = new Dog('Will');
Will.woof(); // prints "MOO" and "My name is Will"
Animal.prototype.talk = function() {
console.log("My name is " + this.name);
}
var will = new Animal('Will');
will.talk(); // My name is Will
var func = will.talk
func(); // My name is
function talkThrice(f) {
f(); f(); f();
}
talkThrice(will.talk); // prints empty lines!
function talkThrice(f) {
f(); f(); f();
}
talkThrice(function(){ will.talk(); });
$('#container').css('width', '100px');
$('.box').animate({ top: '+=50' }, function() {
console.log('Animation finished');
})
$('a').click(function(){
alert('WARNING: LINK CLICKED');
})
$.ajax({
url: 'https://api.forecast.io/forecast/1b7f8a378848cf9eeaff18f73eaed6c5/40.4383,-79.997',
dataType: 'jsonp',
success: function(data) {
var conditions = data.currently;
var message = "In Pittsburgh, it's currently " +
conditions.summary + " and " +
conditions.temperature + " degrees."
alert(message);
}
});
this problem?function talkThrice(f) {
f(); f(); f();
}
talkThrice(_.bind(will.talk, this));
(function() { 'use strict'; var myLocalVariable = ... })();console.log(myLocalVariable) // prints 'undefined'
var Module = (function() { 'use strict'; return { foo: 'bar' } })();console.log(Module.foo) // prints 'bar'
define(function(require) {
'use strict';
var $ = require('jQuery');
$('#yes').html('no');
})
myproject/
-> index.html
-> style.css
-> images/
-> *.(png|jpg|...)
-> js/
-> vendor/
-> 3rd party libraries
-> app.js
-> constants.js
-> MyClass.js
-> other files...
<!DOCTYPE html>
<html>
<head>
<title>My Project</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div id="container">
<!-- body html here -->
</div>
<script data-main="js/app" src="js/vendor/require.js"></script>
</body>
</html>
define(function(require) {
'use strict';
requirejs.config({
urlArgs: 'bust=' + (new Date()).getTime(),
shim: {
'jQuery': { exports: '$' }
},
paths: {
'jQuery': 'vendor/jquery-2.0.3.min',
}
});
require(['Game'], function(Game) {
var gameInstance = new Game();
gameInstance.run();
})
});