Está en la página 1de 4

14.2.

Ejercicio 2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ejercicio 2 - Clases</title>
<script type="text/javascript">
// Definición de la clase Cliente
function Cliente(nombre, direccion, telefono, nif) {
this.nombre = nombre;
this.direccion = direccion;
this.telefono = telefono;
this.nif = nif;
}
// Definición de la clase Elemento
function Elemento(descripcion, cantidad, precio) {
this.descripcion = descripcion;
this.cantidad = cantidad;
this.precio = precio;
}
// Definición de la clase Factura
function Factura(cliente, elementos) {
this.cliente = cliente;
this.elementos = elementos;
this.informacion = {
baseImponible: 0,
iva: 16,
total: 0,
formaPago: "contado"
};
};
// La información de la empresa que emite la factura se
// añade al prototype porque se supone que no cambia
Factura.prototype.empresa = {
nombre: "Nombre de la empresa",
direccion: "Direccion de la empresa",
telefono: "900900900",
nif: "XXXXXXXX"
};
// Métodos añadidos al prototype de la Factura
Factura.prototype.calculaTotal = function() {
Introducción a AJAX Capítulo 14. Ejercicios resueltos
www.librosweb.es 206
for(var i=0; i<this.elementos.length; i++) {
this.informacion.baseImponible += this.elementos[i].cantidad *
this.elementos[i].precio;
}
this.informacion.total = this.informacion.baseImponible * this.informacion.iva;
}
Factura.prototype.muestraTotal = function() {
this.calculaTotal();
alert("TOTAL = " + this.informacion.total + " euros");
}
// Creación de una factura
var elCliente = new Cliente("Cliente 1", "", "", "");
var losElementos = [new Elemento("elemento1", "1", "5"),
new Elemento("elemento2", "2", "12"),
new Elemento("elemento3", "3", "42")
];
var laFactura = new Factura(elCliente, losElementos);
laFactura.muestraTotal();
</script>
</head>
<body>
</body>
</html>
14.3. Ejercicio 3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ejercicio 3 - Prototype</title>
<script type="text/javascript">
// Funcion que añade elementos al final del array
Array.prototype.anadir = function(elemento) {
this[this.length] = elemento;
}
var array1 = [0, 1, 2];
array1.anadir(2);
alert(array1);
// Funcion que añade elementos al final del array y
// permite evitar añadir elementos duplicados
Array.prototype.contiene = function(elemento) {
for(var i=0; i<this.length; i++) {
// Es importante utilizar el operador === para comprobar
// que los elementos sean exactamente iguales
if(this[i] === elemento) {
return true;
}
Introducción a AJAX Capítulo 14. Ejercicios resueltos
www.librosweb.es 207
}
return false;
}
Array.prototype.anadir = function(elemento, permitirDuplicados) {
var permitir = (permitirDuplicados == null) ? true : permitirDuplicados;
if (!permitir) {
if(!(this.contiene(elemento))) {
this[this.length] = elemento;
}
}
else {
this[this.length] = elemento;
}
}
var array2 = [0, 1, 2];
array2.anadir(2);
alert(array2);
var array3 = [0, 1, 2];
array3.anadir(2, false);
alert(array3);
</script>
</head>
<body>
</body>
</html>
14.4. Ejercicio 4
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ejercicio 4 - Prototype</title>
<script type="text/javascript">
// Funcion que trunca la longitud de una cadena
String.prototype.truncar = function(longitud) {
longitud = longitud || 10;
if(this.length > longitud) {
return this.substring(0, longitud);
}
else {
return this;
}
}
var cadena = "hola mundo";
alert(cadena.truncar(6));
// Funcion que trunca la longitud de una cadena y añade
Introducción a AJAX Capítulo 14. Ejercicios resueltos
www.librosweb.es 208
// un indicador de cadena truncada
String.prototype.truncar = function(longitud, indicador) {
longitud = longitud || 10;
indicador = indicador || '...';
if(this.length > longitud) {
return this.substring(0, longitud-indicador.length) + indicador;
}
else {
return this;
}
}
var cadena = "En un lugar de la Mancha, de cuyo nombre no quiero acordarme, no ha
mucho
tiempo que vivía un hidalgo de los de lanza en astillero, adarga antigua, rocín
flaco y
galgo corredor.";
alert(cadena.truncar(50, '... (sigue)'));
alert(cadena.truncar(50));
</script>
</head>
<body>
</body>
</html>

eso de paypal donde esta === tarea

http://www.tutorialesprogramacionya.com/ajaxya/

ajax
https://www.youtube.com/watch?
v=tlkO9hhOqak&list=PLSuKjujFoGJ0XF_Gv0VpiTHxAtO7LL8jl&index=1
playpal pagina wep

Iver: 48272843
Iver: Emisión
Iver: 08 11 2019

También podría gustarte