Articles taggés avec 'Object'


JavaScript Object (Partie I)

Si vous avez besoin d'instancier des objets multiples d'une classe, une méthode consiste à définir la classe à travers la pseudo-constructeur function() .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/ / Définition de classe
param1 , param2 ) { Fonction CMyClass (param1, param2) {
= param1 ; ce myProperty = param1.;
= param1 + param2 ; ce myAdding = param1 param2 +.;
/ /
= function ( param1 ) { ce. myMethod = function (param1) {
"add: " + this . myAdding + " - Param: " + param1 ) ; alert ("ajouter:" + ce + myAdding "- Param:". + param1);
}
}
/ /
new CmyClass ( 10 , 20 ) ; var obj1 = nouvelle CMyClass (10, 20);
new CmyClass ( 20 , 40 ) ; var obj2 = new CMyClass (20, 40);
/ /
"Hi from obj1" ) ; . obj1 myMethod ("Salut à partir obj1");
/ /
"Hi from obj2" ) ; . obj2 myMethod ("Salut à partir obj2");

Définition de la classe - avec beaucoup de paramètres initiaux - Non je ne peux créer des instances de mon objet, tout en créant avec la personnalisation du mot-clé new .
Je peux aussi insérer un appel à une méthode lors de l'initialisation de l'objet, en faisant attention à insérer seulement le dernier - voir la ligne 14:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/ / Définition de classe
param1 , param2 ) { Fonction CMyClass (param1, param2) {
= param1 ; ce myProperty = param1.;
= param1 + param2 ; ce myAdding = param1 param2 +.;
/ /
= function ( param1 ) { ce. myMethod = function (param1) {
"Result: " + this . myResult ) ; alert ("Résultat:". + ce myResult);
}
/ /
function ( ) { ce _init. = function () {
= this . myProperty * 100 ; ce myResult = ce myProperty * 100..;
}

; ce _init ().;

}
/ /
new CmyClass ( 10 , 20 ) ; var obj1 = nouvelle CMyClass (10, 20);
new CmyClass ( 20 , 40 ) ; var obj2 = new CMyClass (20, 40);
/ /
"Hi from obj1" ) ; . obj1 myMethod ("Salut à partir obj1");
/ /
"Hi from obj2" ) ; . obj2 myMethod ("Salut à partir obj2");

Une façon serait une erreur - voir la ligne 6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/ / Définition de classe
param1 , param2 ) { Fonction CMyClass (param1, param2) {
= param1 ; ce myProperty = param1.;
= param1 + param2 ; ce myAdding = param1 param2 +.;

; ce _init ().;

/ /
= function ( param1 ) { ce. myMethod = function (param1) {
"Result: " + this . myResult ) ; alert ("Résultat:". + ce myResult);
}
/ /
function ( ) { ce _init. = function () {
= this . myProperty * 100 ; ce myResult = ce myProperty * 100..;
}


}
/ /
new CmyClass ( 10 , 20 ) ; var obj1 = nouvelle CMyClass (10, 20);
new CmyClass ( 20 , 40 ) ; var obj2 = new CMyClass (20, 40);
/ /
"Hi from obj1" ) ; . obj1 myMethod ("Salut à partir obj1");
/ /
"Hi from obj2" ) ; . obj2 myMethod ("Salut à partir obj2");

Une autre façon de créer un objet à la volée (on-mouche) est de déclarer une variable / fonction, à usage unique:

1
2
3
4
5
6
7
8
9
{ var monObjet = {
, myProperty: 10,

( ) { myMethod: function () {
"Hi from " + this . myProperty ) ; alert ("Salut de" + ce myProperty.);
}
};

; . myObject maMethode ();

Cette technique est utile lorsque vous voulez créer un istranza unique d'un objet, et le traiter comme tel. Ceci est souvent utilisé dans les frameworks Ajax récents comme Prototype et ses dérivés. Partant de là, en fait, il n'est pas possible d'instancier un nouvel objet, puisque la classe a été perdu.

Il peut alors recourir à l'utilisation d'un prototype pour étendre une classe, même vide. Par exemple:

1
2
3
4
5
6
7
8
9
10
11
12
13
Fonction CMyClass () {}

{ CMyClass. Prototype = {
, myProperty: 10,

( ) { myMethod: function () {
"Hi from " + this . myProperty ) ; alert ("Salut de" + ce myProperty.);
}
}

new CmyClass ( ) ; var obj = CMyClass nouvelle ();

; obj maMethode ().;

Ou:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CMyClass fonction () {
= 1000 ; ce myOldProperty = 1000.;
}

{ CMyClass. Prototype = {
, myProperty: 10,

( ) { myMethod: function () {
"Hi from " + this . myProperty ) ; alert ("Salut de" + ce myProperty.);
"Hi from " + this . myOldProperty ) ; alert ("Salut de" + ce myOldProperty.);
}
}

new CmyClass ( ) ; var obj = CMyClass nouvelle ();

; obj maMethode ().;

En savoir plus ...



Arrêtez SOPA