Par
Vivian Pennel / @vp3n
Lead Developer chez IOcean
function Person(firstName) {
this.firstName = firstName;
}
Person.prototype.getFirstName = function() {
return this.firstName();
}
//p a maintenant le prototype Person
// et une propriété constructor affectée à Person()
var p = new Person();
function Employee(isSenior) {
this.isSenior = isSenior;
}
Employee.prototype.isSenior = function() {
return this.isSenior;
}
Employee.prototype = Person.prototype;
Employee.prototype.constuctor = Employee;
var A = function() {}
var a = new A();
console.log(a instanceof A); //true;
console.log(a instanceof Object); //true;
// definit aucun prototype
var animal = Object.create(null);
console.log(animal.prototype); //undefined
animal.color = 'black';
var cat = Object.create(animal);
// Object {color: "black"}
console.log(Object.getPrototypeOf(cat));
cat.age = 12;
//Object {age: 12, color: "black"}
console.log(cat);
var a = Object.create(Object.prototype) <=> var a = {}
//wrapper de fonction, évite de déclarer des variables globales !
// le scope est réduit à la fonction anonyme
(function() {
'use strict';
function myFunction() {
// undefined, sans 'use strict' ce serait égal à window !
alert(this);
}
myFunction();
})(); // exécute la fonction immédiatement
(function() {
'use strict';
var myObject = {
myMethod : function() {
// object - correspond à l'objet myObject
alert(this);
}
}
myObject.myMethod();
})();
(function() {
'use strict';
// object - correspond à une instance de MyConstructor
// La majuscule en début est très importante!
// Seule une convention différencie l'appel
// de constructeur d'une fonction
var MyConstructor = function() {
alert(this);
}
var myObject = new MyConstructor();
})();
(function() {
'use strict';
// La majuscule permet d'identifier
// qu'il s'agit d'un constructeur
// et donc que l'on doit utiliser "new"
var MyConstructor = function() {
alert(this); // undefined !
}
// rien ne nous empêche d'appeller le constructeur
// en mode fonction
var myObject = MyConstructor();
})();
(function() {
'use strict';
function myFunction(arg1, arg2) {
alert(this.myProp); // 1
}
var arg1, arg2;
// On choisit la valeur de this
// principe très utilisé dans des lib comme jQuery
myFunction.call({myProp: 1}, arg1, arg2);
myFunction.apply({myProp: 1}, [arg1, arg2]);
})();
(function() {
'use strict';
// déclaration directe
// pattern "singleton" gratuitement
var myObject = {
myProperty : 2,
myMethod: function() {
alert(this.myProperty); // 2
}
}
myObject.myMethod();
})();
(function() {
'use strict';
// La majuscule permet d'identifier
// qu'il s'agit d'un constructeur
// et donc que l'on doit utiliser "new"
var MyConstructor = function(value) {
this.myProperty = value;
alert(this);
}
MyConstructor.prototype.myMethod() {
alert(this.myProperty);
}
var myObject = new MyConstructor(1); myObject.myMethod(); // 1
myObject = new MyConstructor(1); myObject.myMethod(); // 2
})();
(function() {
'use strict';
function myObject(myParam) {
var myPrivateProperty = myParam + 1;
function myPrivateFunction() {
alert(myPrivateProperty);
}
return {
myPublicProperty : 'abc',
myPublicMethod : function() {
return myPrivateFunction();
}
}
}
var instance = myObject(1);
instance.myPublicMethod(); //2
})();
function Person() {
jQuery("selecteur").click(function(e) {
this.myFunction(); //error myFunction is not defined
});
}
Person.prototype.myFunction = function() {
return 'myValue';
}
var a = new Person();
function Person() {
var self = this; // ou that = this
jQuery("selecteur").click(function(e) {
self.myFunction(); //myValue
});
}
Person.prototype.myFunction = function() {
return 'myValue';
}
var a = new Person();
(function() {
'use strict';
// que va t-il se passer après minification?
var myFunction = function() {
var property;
eval('property = 3');
alert(property); // affiche 3
}
myFunction();
})();
function() {
'use strict';
var arr = ['a','b','c'], indexes = [];
Array.prototype.myProperty = 'myValue';
for (var index in arr) {
indexes.push(index);
}
//["0", "1", "2", "myProperty", myProperty: "myValue"]
console.log(indexes);
})();
function() {
'use strict';
var i;
for(i = 0; i < 10; i++) {
setTimeout(function() {
//qu est-ce qui va être affiché ?
console.log(i);
}, 1000);
}
})();
function() {
'use strict';
var i;
for(i = 0; i < 10; i++) {
(function() { // nouveau scope
var j = i; //capture de la valeur
setTimeout(function() {
console.log(j);
}, 1000);
})();
}
// affiche de 0 à 9
})();
(function() {
'use strict';
console.log(1 == true); //true
console.log(1 === true); //false
console.log(1 == '1'); // true
console.log(1 === '1'); // false
})();
var a = ['a', 'b', 'c', 'd'];
console.log(a); //["a", "b", "c", "d"]
delete(a[2]);
console.log(a); //!! ["a", "b", undefined, "d"]
var a = parseInt('A');
console.log(a); // NaN
console.log(a == NaN); // false
console.log(a === NaN); //false
console.log(NaN !== NaN); // true
console.log(isNaN(a)); // true - Bonne réponse !