IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

[POO] Objet json , comment instancier un nouvel objet ?


Sujet :

JavaScript

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Expert confirmé
    Avatar de le_chomeur
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2006
    Messages
    3 653
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2006
    Messages : 3 653
    Par défaut [POO] Objet json , comment instancier un nouvel objet ?
    bonjour
    je cherche a instancié un objet de type json :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    var toto ={
    monid : null
    }
    si je fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    var moi = toto ;
    moi.monid = 'test';
    alert(moi.monid);
    var newmoi = toto;
    newmoi.monid = 'newtest';
    alert(newmoi.monid);
    alert(moi.monid);
    le second alert(moi.monid); renvoi 'newtest' comment créer un vrai objet et non un pointeur vers l'objet de base ???

  2. #2
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 659
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 659
    Billets dans le blog
    1
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    26
    27
    28
    29
    30
    31
    <script type='text/javascript'>
    function clone (deep) {
      var objectClone = new this.constructor();
      for (var property in this)
        if (!deep)
          objectClone[property] = this[property];
        else if (typeof this[property] == 'object')
          objectClone[property] = this[property].clone(deep);
        else
          objectClone[property] = this[property];
      return objectClone;
    }
    Object.prototype.clone = clone;
     
     
     
    var toto ={
    monid : null
    }
     
     
    var moi = toto ;
    moi.monid = 'test';
    alert(moi.monid);
     
    var newmoi =  moi.clone();
    newmoi.monid = 'newtest';
     
    alert(newmoi.monid);
    alert(moi.monid);
    </script>
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  3. #3
    Expert confirmé
    Avatar de le_chomeur
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2006
    Messages
    3 653
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2006
    Messages : 3 653
    Par défaut
    Merci spaffy
    le roi du fil a couper la brique

  4. #4
    Expert confirmé
    Avatar de le_chomeur
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2006
    Messages
    3 653
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2006
    Messages : 3 653
    Par défaut


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
     
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    	.slideIn{
    		overflow:hidden;
    		width:500px;
    		height:300px;
    		background-color:green;
    		position:relative;
    	}
    	.slideIn div{
    		width:100%;
    		height:300px;
    		position:absolute;
    		display:none;
    	}
    </style>
    <script type="text/javascript">
     
    function Create(deep) {
      var objectClone = new this.constructor();
      for (var property in this)
        if (!deep)
          objectClone[property] = this[property];
        else if (typeof this[property] == 'object')
          objectClone[property] = this[property].Create(deep);
        else
          objectClone[property] = this[property];
      return objectClone;
    }
    Object.prototype.Create = Create;
     
     
    var SlideIn = {
    	//Id du conteneur
    	idObj : null
    	,
    	timer : null
    	,
    	from : null
    	,
    	to : null
    	,
    	direction : null
    	,
    	//Tableau d'élément a déplacé
    	elementToSlide : new Array()
    	,
    	//Index de l'élément en cours
    	currentIndex : 0
    	,
    	$:function(element){
    		return document.getElementById(element);
    	}
    	,
    	SlideToLeft : function(){
    		if(this.timer != null){
    			clearTimeout(this.timer);
    			this.timer = null;
    		}
    		//On vérifit la direction pour initialiser le positionnement
    		if(this.direction != 'left'){
    			this.direction = 'left';
    			this.Positionne();
    		}
    		//Si le timer n'est pas finit on détruit l'ancienne div
    		if(parseInt(this.from.style.left) == Number.NaN || (parseInt(this.from.offsetWidth) + parseInt(this.from.style.left))> 0){
    			this.from.style.left = parseInt(this.from.style.left) - 20 + "px";
    			this.to.style.left  =parseInt(this.to.style.left) - 20 + "px";
    			//alert((parseInt(this.$(from).offsetWidth) + parseInt(this.$(from).style.left)));
    			var me = this ;
    			this.timer = setTimeout(function(){me.SlideToLeft()},30);
    		}
    		else{
    			clearTimeout(this.timer);
    			this.timer = null;
    			this.currentIndex = (this.currentIndex == (this.elementToSlide.length-1)) ? 0:this.currentIndex + 1;
    			this.$('debug').innerHTML = this.currentIndex;
    			this.Positionne();
    			//TimerRunning = false;
    			//$(smallMenu).parentNode.removeChild($(smallMenu));
    		}
    	}
    	,
    	SlideToRight : function(){
    		if(this.timer != null){
    			clearTimeout(this.timer);
    			this.timer = null;
    		}
    		if(this.direction != 'right'){
    			this.direction = 'right';
    			this.Positionne();
    		}
    		//Si le timer n'est pas finit on détruit l'ancienne div
    		if(parseInt(this.from.style.left) == Number.NaN ||  parseInt(this.from.style.left) < parseInt(this.from.offsetWidth)){
    			this.from.style.left = parseInt(this.from.style.left) + 20 + "px";
    			this.to.style.left  =parseInt(this.to.style.left) + 20 + "px";
    			//alert((parseInt(this.$(from).offsetWidth) + parseInt(this.$(from).style.left)));
    			var me = this ;
    			this.timer = setTimeout(function(){me.SlideToRight()},30);
    		}
    		else{
    			clearTimeout(this.timer);
    			this.timer = null;
    			this.currentIndex = (this.currentIndex == 0) ? this.elementToSlide.length-1:this.currentIndex - 1;
    			this.$('debug').innerHTML = this.currentIndex;
    			this.Positionne();
    		}
    	}
    	,
    	//Fonction initialisant le tableau en positionnant tous les éléments :)
    	Positionne : function(){
    		if(this.direction == 'left'){
    			//On vérifit que l'on est pas a la fin sinon le premier devient le dernier
    			if(this.currentIndex == this.elementToSlide.length-1){
    				//récupération des éléments : 
    				this.from = this.$(this.elementToSlide[this.currentIndex]);
    				this.to = this.$(this.elementToSlide[0]); //Premier élément
    			}
    			else{
    				this.from = this.$(this.elementToSlide[this.currentIndex]);
    				this.to = this.$(this.elementToSlide[this.currentIndex + 1]);
    			}
    				this.from.style.display = "block" ;
    				this.from.style.left = 0 + "px";
    				this.to.style.left = this.from.offsetWidth + "px";
    				this.to.style.display = "block"
    		}
    		else{
    			if(this.currentIndex == 0){
    				this.from = this.$(this.elementToSlide[this.currentIndex]);
    				this.to = this.$(this.elementToSlide[this.elementToSlide.length-1]); // dernier élément
    			}
    			else{
    				this.from = this.$(this.elementToSlide[this.currentIndex]);
    				this.to = this.$(this.elementToSlide[this.currentIndex-1]);
    			}
    			this.from.style.display = "block" ;
    			this.from.style.left = 0 + "px";
    			this.to.style.left = - (this.from.offsetWidth )+ "px";
    			this.to.style.display = "block";
    		}
    	}
     
    }
     
    var newSlide = SlideIn;
     
    newSlide.elementToSlide.push('la1');
    newSlide.elementToSlide.push('la2');
    newSlide.elementToSlide.push('la3');
    newSlide.elementToSlide.push('la4');
    newSlide.elementToSlide.push('la5');
    newSlide.elementToSlide.push('la6');
     
     
    var newSlide2 = newSlide.Create();
     
    newSlide2.elementToSlide.push('la11');
    newSlide2.elementToSlide.push('la22');
    newSlide2.elementToSlide.push('la33');
    newSlide2.elementToSlide.push('la44');
    newSlide2.elementToSlide.push('la55');
    newSlide2.elementToSlide.push('la66');
    </script>
    </head>
    <body>
     
    <div class="slideIn">
    	<div id="la1" style="background-color:red;left:0px;"></div>
    	<div id="la2" style="background-color:blue;left:0px;">la</div>
    	<div id="la3" style="background-color:#ccc;left:0px;">la</div>
    	<div id="la4" style="background-color:yellow;left:0px;"></div>
    	<div id="la5" style="background-color:black;left:0px;">la</div>
    	<div id="la6" style="background-color:pink;left:0px;">la</div>
    </div>
    <input type="button" value="gauche" onclick="newSlide.SlideToLeft()">
    <input type="button" value="droite" onclick="newSlide.SlideToRight()">
    <span id="debug"></span>
    <div class="slideIn">
    	<div id="la11" style="background-color:red;left:0px;"></div>
    	<div id="la22" style="background-color:blue;left:0px;">la</div>
    	<div id="la33" style="background-color:#ccc;left:0px;">la</div>
    	<div id="la44" style="background-color:yellow;left:0px;"></div>
    	<div id="la55" style="background-color:black;left:0px;">la</div>
    	<div id="la66" style="background-color:pink;left:0px;">la</div>
    </div>
    <input type="button" value="gauche" onclick="newSlide2.SlideToLeft()">
    <input type="button" value="droite" onclick="newSlide2.SlideToRight()">
    </body>
    </html>
    on voit que le tableau fait 10

  5. #5
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 659
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 659
    Billets dans le blog
    1
    Par défaut
    C'est supposé faire quoi ?
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  6. #6
    Expert confirmé
    Avatar de le_chomeur
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2006
    Messages
    3 653
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2006
    Messages : 3 653
    Par défaut
    lol faire défiler les div :

    suite a cette discution : http://www.developpez.net/forums/d63...orizontal-fin/

Discussions similaires

  1. Réponses: 2
    Dernier message: 30/08/2011, 23h40
  2. Perte du Binding si l'on instancie un nouvel objet, est ce normal?
    Par takinelinfo dans le forum Windows Presentation Foundation
    Réponses: 2
    Dernier message: 09/06/2011, 15h56
  3. Instancier un nouvel objet en php
    Par abderrahmen dans le forum Langage
    Réponses: 1
    Dernier message: 20/10/2008, 21h53
  4. Instancier un nouvel Objet de Classe avec un String
    Par Cribest dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 15/07/2008, 15h56
  5. Explorateur d'Objets VBA comment Instancier ?
    Par jacou dans le forum VBA Access
    Réponses: 6
    Dernier message: 10/11/2007, 00h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo