window.color = “red”; //<-- general scope everything without a scope
var object = { color: “blue” };
function sayColor(){
//alert(this.color);
}
sayColor(); //”red” without scope
o.sayColor = sayColor;
o.sayColor(); //”blue” inside a object/function
viernes, 16 de noviembre de 2012
Scopes in Javascript
Function Declarations vs. Function Expressions
A Function Declaration is:
function add(numberOne, numberTwo){
return numberOne + numberTwo;
}
A Function Expressions is:
var add = function(numberOne, numberTwo){
return numberOne + numberTow;
};
When we write a function declaration, it's loaded in the execution context, before the script runs, but when we write a function expresion, the interpreter, load while it's executing the code. So we can find a simple error.
//alert(add(10,10)); the vm doesn't found the function add(), because it's not loaded
var add = function(numberOne, numberTwo){
return numberOne + numberTow;
};
My first Class in Javascript: Manage the Html tag select
//objeto que describe cada uno de los
//options del Select|Combo
function ComboOption(texto, valor) {
this.texto = texto;
this.valor = valor;
}
//Gestiona las posibles operaciones sobre el control
//HTML
function GestorCombo(idCombo) {
this.combo = document.getElementById(idCombo);
if (this.combo == null) { return; }
this.borrarTodasLasOpciones = function () {
var i;
for (i = this.combo.length - 1; i >= 0; i--) {
if (this.combo.options[i].selected) {
this.combo.remove(i);
}
}
}
this.aniadirOption = function (option) {
this.combo.options[this.combo.options.length] = new Option(option.texto, option.valor);
}
this.conseguirValorSeleccionado = function () {
var resultado = '';
if ((this.combo.selectedIndex > -1) && (this.combo.options.length > 0)) {
resultado = this.combo.options[this.combo.selectedIndex].value;
}
return resultado;
}
this.seleccionarOption = function (valor) {
for (i = 0; i < this.combo.options.length; i++) {
if (this.combo.options[i].value == valor) { break; }
}
this.combo.options.selectedIndex = i;
}
}
martes, 13 de noviembre de 2012
Ordering Asc & Desc In Javascript
It's aMazing and so easy!!! Here it's the code
But We can improve it, in this way ...
function orderDescending(valueOne, valueTwo){
if(valueTwo > valueOne){
return 1;
}
if (valueTwo < valueOne) {
return -1;
}
else{return 0;}
}
function orderAscending(valueOne, valueTwo){
if(valueTwo > valueOne){
return -1;
}
if (valueTwo < valueOne) {
return 1;
}
else{return 0;}
}
var numbers = [1,4,27,4,8,0];
numbers.sort(orderAscending);
//alert(numbers);
numbers.sort(orderDescending);
//alert(numbers);
But We can improve it, in this way ...
function Comparator(propertyName) {
return function(objectOne, objectTwo){
var valueOne = objectOne[propertyName];
var valueTwo = objectTwo[propertyName];
if (value1 < value2){
return -1;
} else if (value1 > value2){
return 1;
} else {
return 0;
}
};
}
And We can use it in this way
var data = [{name: “PersonOne”, age: 1}, {name: “PersonTwo”, age: 2}];
data.sort(Comparator(“name”));
jueves, 8 de noviembre de 2012
Working With JQueryDataTable data
I´m using JQuery-DataTable in a SharePoint 2010 Application. I comunicate server to client throught $.getJSON(url, parameter, function(returnData){}); But when I pass the JSON to JQueryDatatable, it doesn´t work, why? I don´t Know. It works, with arrays, not with objects, I don´t why. But we must convert the JSON Object into Array. So We have to use this simple function
function ConvertObjectToArray(obj) {
var arr = [];
for (cont = 0; cont < obj.length; cont++) {
var t = 0;
arr[cont] = [obj.length];
for (var propiedad in obj[cont]) {
arr[cont][t] = obj[cont][propiedad];
t++;
}
}
return arr;
}
viernes, 2 de noviembre de 2012
Suscribirse a:
Entradas (Atom)