Horváth, Győző
senior lecturer
horvath.gyozo@inf.elte.hu
Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government
//Boolean
true, false
//Numbers
12, 56.432
//Strings
'apple', "pear"
//Special
undefined, null
//Arrays
[1, 'two', true]
//Objects
{
prop1: 'value1',
prop2: 'value2'
}
//Variable with initial value
var name = 'Winnie the Pooh';
//Variable without initial value
var another; //undefined
//Multiple variable definitions
var alma = 'piros',
korte = 'sárga';
//The === operator
'1' == 1 //true
'1' === 1 //false
'1' !== 1 //true
//Conditional controls
if (cond) {
statements
}
if (cond) {
statements
} else {
statements
}
//Iterative controls
while (cond) {
statements
}
do {
statements
} while (cond);
for (var i = 1; i <= n; i++) {
statements
}
for (var prop in obj) {
statements
}
//Global variable
var glob = 42;
//Function declaration
function funcName(par1, par2) {
var localVar = 12;
statements;
return returnValue;
}
var obj = {
property: 1,
'this is a property too': 2,
method: function () {
console.log(this.property);
}
};
obj.property
obj['this is a property too']
obj.method()
//Creating an object
var obj = {
a: 1,
b: 2
};
//New property
obj.c = 3;
//Accessing property (read)
obj.a === 1
obj['a'] === 1
obj.d === undefined
//Modifying the value of a property (write)
obj.b = 42;
//Deleting a property
delete obj.c;
//Setting up the prototype chain
var obj2 = Object.create(obj1);
//Getting the prototype object
obj1.isPrototypeOf(obj2) //obj1 is in the prototype chain of obj2
Object.getPrototypeOf(obj2) === obj1 //obj2 is the prototype object of obj1
var uniqueObject = {
name: 'Peter',
describe: function() {
return 'Person called '+this.name;
}
};
uniqueObject.describe(); //Person called Peter
function personGenerator(name) {
return {
name: name,
describe: function() {
return 'Person called '+this.name;
}
};
}
var peter = personGenerator('Peter');
var julia = personGenerator('Julia');
(We do not follow the object generator path below, instead we focus on constructor functions...)
function Person(name) {
this.name = name;
this.describe = function () {
return 'Person called '+this.name;
};
}
var jim = new Person('Jim');
jim.name //'Jim'
jim.describe() //'Person called Jim'
Putting methods in the prototype is more efficient
function Person(name) {
this.name = name;
}
Person.prototype.describe = function () {
return 'Person called '+this.name;
};
var jim = new Person('Jim');
jim.name //'Jim'
jim.describe() //'Person called Jim'
Person and Employee examples are from Speaking JavaScript from Axel Rauschmayer
function Employee(name, title) {
Person.call(this, name);
this.title = title;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.describe = function () {
return Person.prototype.describe.call(this)+' ('+this.title+')';
};
var jane = new Employee('Jane', 'CTO');
jane.describe() //'Person called Jane (CTO)'
function subclasses(SubC, SuperC) {
var subProto = Object.create(SuperC.prototype);
// Save `constructor` and, possibly, other methods
copyOwnPropertiesFrom(subProto, SubC.prototype);
SubC.prototype = subProto;
SubC._super = SuperC.prototype;
};
function Employee(name, title) {
Employee._super.constructor.call(this, name);
this.title = title;
}
Employee.prototype.describe = function () {
return Employee._super.describe.call(this)+' ('+this.title+')';
};
subclasses(Employee, Person);
//Basic constructor
var Person = (function () {
function Person(name) {
this.name = name;
}
Person.prototype.describe = function () {
return 'Person called '+this.name;
};
return Person;
})();
//Inheritance
var Employee = (function (Parent) {
function Employee(name, title) {
Parent.call(this, name);
this.title = title;
}
Employee.prototype.describe = function () {
return Employee._super.describe.call(this)+' ('+this.title+')';
};
subclasses(Employee, Parent);
})(Person);
class Person {
constructor(name) {
this.name = name;
}
describe() {
return 'Person called '+this.name;
}
}
//Inheritance
class Employee extends Person {
constructor(name, title) {
super(name);
this.title = title;
}
describe() {
return super.describe() + ' (' + this.title + ')';
};
}
var emptyArray = [];
var myArr = [12, 'apple', true];
myArr[0]; // => 12;
myArr[1]; // => 'apple';
myArr[2]; // => true;
myArr.length // => 3
//Modification
myArr[0] = 13;
//Expansion
myArr[myArr.length] = 'new';
myArr[100] = 'far away';
myArr.length; // => 101
myArr[99]; // => undefined
//Deletion
delete myArr[1];
myArr[1]; // => undefined
myArr.length; // => 101
pop()
, push(e)
, shift(e)
, unshift()
: modifying both endsreverse()
splice(from, count)
: cutting (and deleting) the inner part of the arrayjoin(separator)
: gluing together the elements with the separator string.forEach
: iteratingmap
: copyingfilter
: filteringevery
: optimistic decisionsome
: decisionreduce
: summing upvar numbers = [1, 2, 3];
var oddNumbers = numbers.filter(function (item) {
return item % 2 !== 0;
});
oddNumbers; //[1, 3]
var fruits = [
'apple',
'pear',
'plum'
];
//Writing the fruits to the console
for (var i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
//or
fruits.forEach(function (fruit) {
console.log(fruit);
});
//or
for (var f of fruits) {
console.log(f);
}
//Declaration
function osszead(a, b) {
return a + b;
}
//With function expression
var osszead = function(a, b) {
return a + b;
}
//ES6 fat arrow syntax
var osszead = (a, b) => a + b;
function [name]([param [, param [..., param]]]) {
statements
}
function linearSearch(x, T) {
var i = 0;
while (i < x.length && !T(x[i])) {
i++;
}
return {
isExist: i < x.length,
offset: i
};
}
function isNegative(p) {
return p < 0;
}
var myArr = [1, 3, -2, 8];
linearSearch(myArr, isNegative);
function operationGenerator(op) {
if (op === '+') {
return function (a, b) {
return a + b;
};
}
else if (op === '*') {
return function (a, b) {
return a * b;
};
}
}
//Generating a summing function
var operation = operationGenerator('+');
operation(10, 32); //42
//Generating a multiplier function
var operation = operationGenerator('*');
operation(10, 32); //320
Every function remain in connection with the containing function, even if the external function ends.
function createIncrementor(start) {
return function () { // (1)
start++;
return start;
}
}
var inc = createIncrementor(5);
inc() // 6
inc() // 7
inc() // 8
this
//Global
var name = 'Peter';
function hello() {
return this.name; // this === global (window)
}
hello();
//Method call
var peter = {
name: 'Peter',
describe: function () {
return this.name; // this === peter
}
}
peter.describe();
//Constructor call
var Person = function(name) {
this.name = name; // this === instance object
}
var peter = new Person('Peter');
//Setting the context of this with call and apply
var peter = {
name: 'Peter',
hello: function () {
return this.name;
}
};
var julia = {
name: 'Julia',
hello: function () {
return this.name;
}
};
peter.hello.call(julia); // "Julia"
var peter = {
name: 'Peter',
age: 42,
describe: function () {
function getAge() {
return this.age; // this === global (window)
}
return this.name + ':' + getAge();
}
}
peter.describe(); // "Peter:undefined"
//With call and apply
var peter = {
name: 'Peter',
age: 42,
describe: function () {
function getAge() {
return this.age; // this depends on the call
}
return this.name + ':' + getAge.call(this);
}
}
peter.describe(); // "Peter:42"
//ES5 bind()
var peter = {
name: 'Peter',
age: 42,
describe: function () {
var getAge = (function () {
return this.age; // inner this is always outer this
}).bind(this);
return this.name + ':' + getAge();
}
}
peter.describe(); // "Peter:42"
//ES6 fat arrow syntax
var peter = {
name: 'Peter',
age: 42,
describe: function () {
var getAge = () => this.age;
return this.name + ':' + getAge();
}
}
peter.describe(); // "Peter:42"