Is it related to Java? A subset maybe?
It has nothing to do with Java
Is it just a scripting language?
Not a real programming language?
It is a complete programming language
prefix and suffix
Programmers don't even bother to learn it
They can be passed as arguments and return values
Easier - more expressive
General containers = hash tables
Very expressive
0.1 + 0.2 // 0.30000000000000004
2 * 'a' // NaN
5 + NaN // NaN
NaN == NaN // false
isNaN(NaN) // true
1 / 0 // Infinity
1 / Infinity // 0
1.79769313486231570e+308
1.8e308 // Infinity
Number('') // 0
Number('0032') // 32
Number('123abc') // NaN
Number('abc') // NaN
+'' // 0
+'0032' // 32
+'123abc' // NaN
+'abc' // NaN
parseInt('123abc', 10) // 123
parseInt('32') // 32 (as decimal)
parseInt('0032') // 26 (as octal if starts with 0)
parseInt('0032', 10) // 32
parseInt('0032', 8) // 26
abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random, round, sin, sqrt, tan
Math.floor(12.53) // 12
E, LN10, LN2, LOG10E, LOG2E, PI, SQRT1_2, SQRT2
Math.PI // 3.141592653589793
'abc' == "abc" // true
'abc'.length // 3
'Java' + 'Script' // 'JavaScript'
String(123) // '123'
'cat'.toUpperCase() // 'CAT'
Boolean(truthy_value) // true
!! falsy_value // false
!! false // false
!! null // false
!! undefined // false
!! '' // false
!! 0 // false
!! NaN // false
!! '0' // true
var empty_object = {};
var person = { 'first-name': 'John' };
var city = {
name: 'Alexandria',
location: { latitude: 31.2, longitude: 29.9 }
};
person['first-name'] // 'John'
city['name'] // 'Alexandria'
city.name // 'Alexandria'
city.location.latitude // 31.2
person.nickname // undefined
var name = person.nickname || 'anonymous'; // 'anonymous'
city.country // undefined
city.country.name // throws a TypeError
city.country && city.country.name // undefined
person['first-name'] = 'Thomas'; // Replaced 'first-name'
person['last-name'] = 'Anderson'; // Added 'last-name'
var x = person; // x & person refer to the same object
x.nickname = 'Neo';
person.nickname // 'Neo'
var a = {}, b = {}, c = {}; // different empty objects
var a = b = c = {}; // the same empty object
Object.prototype.foo = 'bar';
var a = {}, b = {};
a.foo // 'bar'
b.foo // 'bar'
a.foo = 'blah'; // Added foo property to a
a.foo // 'blah'
b.foo // 'bar'
Object.create
function (in new JS editions)if (typeof Object.create != 'function') {
Object.create = function(o) {
var F = function F(){};
F.prototype = o;
return new F();
};
}
var p = {foo: 'bar'};
var a = Object.create(p);
a.foo // 'bar'
Object.prototype.foo = 'bar';
var a = {foo: 'blah'}, b = {};
a.foo // 'blah'
b.foo // 'bar'
a.hasOwnProperty('foo') // true
b.hasOwnProperty('foo') // false
var someone = {'first-name': 'John', 'last-name': 'Doe'};
var person = Object.create(someone);
person.profession = 'artist';
person.age = 30;
for (p in person) {
var mark = person.hasOwnProperty(p) ? ' ' : '* '
console.log(mark + p + ': ' + person[p]);
}
// profession: artist
// age: 30
// * first-name: John
// * last-name: Doe
delete
removes a property from an objectdelete person.age;
person.age // undefined
person['first-name'] = 'Jason';
person['first-name'] // 'Jason'
delete person['first-name']
person['first-name'] // 'John'
var MyAPP = {};
MyAPP.someone = {'first-name': 'John', 'last-name': 'Doe'};
MyApp.person = Object.create(MyAPP.someone);
var add = function (a, b) {
return a + b;
};
this
& arguments
undefined
this
will be the objectvar myObject = {
value: 0,
increment: function (inc) {
this.value += inc;
}
};
myObject.increment(2);
myObject.value; // 3
this
will be the global object (design mistake)myObject.double = function () {
var that = this; // Workaround.
var helper = function () {
that.value = 2 * that.value
};
helper(); // Invoke helper as a function.
};
myObject.double(); // Invoke double as a method.
myObject.getValue(); // 4
new
prefixthis
is bound to the new objectvar Car = function (speed) {
this.speed = speed;
};
Car.prototype.move = function() {
return 'moving at ' + this.speed;
};
var my_car = new Car('40 k/h');
my_car.move(); // moving at 40 k/h
apply
is a method of functionsthis
& array of argumentsvar util = {
get_status: function(uppercase){
return uppercase ?
this.status.toUpperCase() : this.status;
}
};
var obj = { status: 'ok' };
util.get_status.apply(obj, [true]); // 'OK'
arguments
: array of parameters passed to a function
var sum = function () {
var i, s = 0;
for (i = 0; i < arguments.length; i += 1) {
s += arguments[i];
}
return s;
};
sum(4, 8, 15, 16, 23, 42) // 108
undefined
is returnednew
and no return is given the new object is returnedvar Car = function (speed) {
this.speed = speed;
};
var my_car = new Car('40 k/h');
var foo = function () {
var a = 3, b = 5;
var bar = function () {
var b = 7, c = 11;
a += b + c;
};
bar();
};
var elevator_maker = function() {
var level = 1;
var get_level = function() { return "level: " + level; }
return {
up: function(n) { level += n; return get_level(); },
down: function(n) { level -= n; return get_level(); },
};
};
var elevator = elevator_maker();
elevator.up(3); // level: 4
elevator.down(1); // level: 3
var elevator = function() {
var level = 1;
var get_level = function() { return "level: " + level; }
return {
up: function(n) { level += n; return get_level(); },
down: function(n) { level -= n; return get_level(); },
};
}();
elevator.up(3); // level: 4
elevator.down(1); // level: 3
var fibonacci = function (n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
};
var fibonacci = function () {
var memo = {0: 0, 1: 1};
return function fib (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = fib(n - 1) + fib(n - 2);
memo[n] = result;
}
return result;
};
}();
fibonacci(10) // 55
new
prefixvar Vehicle = function(name){ this.name = name; };
Vehicle.prototype.get_name = function(){ return this.name; };
var vehicle = new Vehicle('Just a ride');
var Car = function(name, racing){
this.name = name;
this.racing = racing;
};
Car.prototype = vehicle;
var my_car = new Car('My car', false);
my_car.get_name(); // 'My car'
my_car.racing; // false
var vehicle = {
name: 'Just a ride',
get_name: function(){ return this.name; }
};
var my_car = Object.create(vehicle);
my_car.name = 'My car'
my_car.racing = false;
my_car.get_name(); // 'My car'
my_car.racing; // false
var vehicle = function(spec) {
spec.name = spec.name || 'Just a ride';
var that = {};
that.get_name = function(){ return spec.name; };
return that;
};
var car = function(spec) {
spec.racing = !! spec.racing;
var that = vehicle(spec);
that.is_racing = function(){ return spec.racing; };
return that;
};
my_car = car({name: 'My car', racing: false});
my_car.get_name(); // 'My car'
my_car.is_racing(); // false
var a = [];
var misc = [ 'string', 98.6, true, null, undefined,
['nested', 'array'], {x: 1}, Infinity ];
var a = [];
a.length // 0
a[10] = 1;
a.length // 11
a[4] // undefined
var a = [];
a.push(1);
a // [1]
return
{
status: true
};
return {
status: true
};
1 + 2 // 3
'1' + 2 // '12'
1 + '2' // '12'
typeof null // object
typeof [] // object
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true
/
#