JSOO - JavaScript Orientado a Objeto

Publicado por Emiliano Eloi Silva Barbosa 14/08/2006

[ Hits: 8.153 ]

Homepage: http://emilianoeloi.com.br

Download POO.htm




Um exemplo de um JavaScript orientado a objetos

  



Esconder código-fonte

<HTML>
  <HEAD>
    <TITLE>Classe Ítem</TITLE>
    <SCRIPT>
/**
* @description Classe Item. Manipulação de itens
*
* @package Teste
* @since 2006-04-27
* @author Emiliano ESB <emiliano@portinfo.com.br>
* @access public
*/
function Item(id, description, quantity, price) {
   this.id = id;
   this.description = description;
   if(quantity >=0)
      this.quantity = quantity;
   else
      this.quantity = 0;
   this.unit_price = price;
   this.discount;
   /**
    * Acessor getAdjustedTotal
   */
   this.getAdjustedTotal = function() {
      total = this.unit_price * this.quantity
      total_discount = total * this.discount
      adjusted_total = total - total_discount
      return adjusted_total
   }
   /**
    * Mutante setDiscount
   */
   this.setDiscount = function(discount) {
      if(discount <= 1)
         this.discount = discount
      else
         this.discount = 0   
   }
   /**
    * Acessor getDiscount
   */
   this.getDiscount = function() {
      return discount
   }
   /**
    * Acessor getQuantity
   */
   this.getQuantity = function() {
      return quantity
   }
   /**
    * Mutante setQuantity
   */
   this.setQuantity = function(quantity) {
      if(quantity>=0)
         quantity = quantity
   }
   /**
    * Acessor getProductID
   */
   this.getProductID = function() {
      return id
   }
   /**
    * Acessor getDescription
   */
   this.getDescription = function() {
      return description
   }
}
var objMilk = new Item('diary-011','1 Gallon Milk',2,2.50)
objMilk.setDiscount(0.15)
milk_price = objMilk.getAdjustedTotal();
SAIDA = "Obrigado pela sua compra\n"
SAIDA += "Por favor, volte!\n"
SAIDA += objMilk.getDescription() + "\t" + milk_price + "\n"
SAIDA += "Preço total \t" + milk_price
alert(SAIDA)

    </SCRIPT>
  </HEAD>
  <BODY>
  </BODY>
</HTML>

Scripts recomendados

Refresh

"Apontar"-links para um iframe

Calculadora em JavaScript e HTML/CSS

Data base em HTML para ASP

tremer o browser quando o visitante acessar seu site


  

Comentários
[1] Comentário enviado por mailson em 26/08/2006 - 00:07h

Valeu cara. Teu exemplo me ajudou bastante. Eu tava tentando usar orientação a objeto no JS usando o tutorial da W3Schools (www.w3schools.com) mas não conseguia de maneira nenhuma. O seu tutorial me ajudou muito

PS.: Me cadastrei no site só pra postar esse comentário :D

[2] Comentário enviado por mvleandro em 07/04/2007 - 13:18h

Olá kra! Tenho um artigo muito bom falando sobre javascript orientado a objetos. Espero poder ajudar.

http://www.prosadigital.com/2007/04/uml-e-javascript-captulo-1.html

Grande abraço!


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts