MACROMEDIA FLASH COMMUNICATION SERVER MX-SERVER-SIDE COMMUNICATION ACTIONSCRIPT DICTIONARY Specifiche Pagina 325

  • Scaricare
  • Aggiungi ai miei manuali
  • Stampa
  • Pagina
    / 369
  • Indice
  • SEGNALIBRI
  • Valutato. / 5. Basato su recensioni clienti
Vedere la pagina 324
Server-Side Communication ActionScript 25
If all instances of the Client object (each client in an application) require the same methods or
properties, you can add those methods and properties to the class itself instead of adding them to
each instance of a class. This process is called extending a class. You can extend any server-side or
client-side ActionScript class. To extend a class, instead of defining methods inside the
constructor function of the class or assigning them to individual instances of the class, you assign
methods to the
prototype property of the constructor function of the class. When you assign
methods and properties to the
prototype property, the methods are automatically available to all
instances of the class.
Extending a class lets you define the methods of a class separately, which makes them easier to
read in a script. And more importantly, it is more efficient to add methods to
prototype,
otherwise the methods are re-interpreted each time an instance is created.
The following code shows how to assign methods and properties to an instance of a class. During
application.connect, a client instance clientObj is passed to the server-side script as a
parameter. You can then assign a property and method to the client instance:
application.onConnect = function(clientObj){
}
clientObj.birthday = myBDay;
clientObj.calculateDaysUntilBirthday = function(){
// insert code here
}
The above example works, but must be executed every time a client connects. If you want the
same methods and properties to be available to all clients in the
application.clients array
without defining them every time, you must assign them to the prototype property of the Client
object There are two steps to extending a built-in class using the
prototype method. You can
write the steps in any order in your script. The following example extends the built-in Client
object, so the first step is to write the function that you will assign to the
prototype property:
// first step: write the functions
function Client_getWritePermission(){
// "writeAccess" property is already built-in to the client class
return this.writeAccess;
}
function Client_createUniqueID(){
var ipStr = this.ip;
// "ip" property is already built-in to the client class
var uniqueID = "re123mn"
// you would need to write code in the above line
// that creates a unique ID for each client instance
return uniqueID;
}
// second step: assign prototype methods to the functions
Client.prototype.getWritePermission = Client_getWritePermission;
Client.prototype.createUniqueID = Client_createFriendlyUniqueID;
// a good naming convention is to start all class method
// names with the name of the class, followed by an underscore
You can also add properties to prototype, as shown in the following example:
Client.prototype.company = "Macromedia";
Vedere la pagina 324
1 2 ... 320 321 322 323 324 325 326 327 328 329 330 ... 368 369

Commenti su questo manuale

Nessun commento