Ejercicios

  1. ¿Cual es la salida?
    > z
    { x: 3, y: 1 }
    > Object.keys(z)
    ?????
    > Object.keys(z).forEach(function(i) { console.log(i+" -> "+z[i]); })
    ?????
    

  2. ¿Que queda finalmente en z?
    > z
    { x: 2, y: 1 }
    > Object.defineProperty(z, 'y', {writable : false})
    { x: 2, y: 1 }
    > z.x = 3
    ??????
    > z.y = 2
    ??????
    > z
    

  3. ¿Cuales son las salidas?
    > obj = { x : 1, y : 2}
    { x: 1, y: 2 }
    > bValue = 5
    5
    > Object.defineProperty(obj, "b", {get: function(){ return bValue; }, 
                                       set: function(y){ bValue = y; }}) 
    { x: 1, y: 2 }
    > obj.b = "hello"
    
    > bValue
    
    > obj.b
    
    > bValue = "world"
    
    > obj.b
    
    >
    
  4. ¿Cuales son las salidas?
    > var o = {};
    undefined
    > Object.defineProperty(o, "a", { value : 1, enumerable:true });
    { a: 1 }
    > Object.defineProperty(o, "b", { value : 2, enumerable:false });
    { a: 1 }
    > Object.defineProperty(o, "c", { value : 3 }); // enumerable defaults to false
    { a: 1 }
    > o.d = 4; // enumerable defaults to true when creating a property by setting it
    4
    >  
    undefined
    > for (var i in o) {    
    ...   console.log(i);  
    ... }
    ???????
    > Object.keys(o); 
    ???????
    > o.propertyIsEnumerable('a'); 
    ????
    > o.propertyIsEnumerable('b');
    ????
    > o.propertyIsEnumerable('c');
    ????
    > o.b
    ????
    > o["b"]
    ????
    
  5. > function foo(a, b){return a * b;}
    undefined
    > f = function foo(a, b){return a * b;}
    [Function: foo]
    > foo.length
    2
    > foo.constructor
    [Function: Function]
    > foo.prototype
    {}
    > typeof foo.prototype
    'object'
    > [1, 2].constructor
    [Function: Array]
    
  6. ¿Cuales son las salidas?
    > Object.getPrototypeOf({ a: 1})
    
    > Object.getPrototypeOf([1,2,3])
    
    > Object.getPrototypeOf([1,2,3]) == Array.prototype
    
    > Object.getPrototypeOf({ a:1 }) === Object.prototype
    
    > Object.getPrototypeOf(Array.prototype)
    
    > Object.getPrototypeOf(Object.prototype)
    
    > Object.getPrototypeOf(function() {})
    
    > Object.getPrototypeOf(Object.getPrototypeOf(function() {}))
    
    > [1,2,3].__proto__
    
    > [1,2,3].__proto__ == Array.prototype
    

Figura: Jerarquía de Prototipos Nativos
\begin{figure}\begin{center}
\centerline{\epsfig{file=chapter1/javascript_natives.eps, width=12cm}}
\end{center}
\end{figure}

var b = new Foo(20);
var c = new Foo(30);
Figura 6.2: __proto__ and prototypes
\begin{figure}\begin{center}
\centerline{\epsfig{file=chapter1/proto_and_prototypes.eps, width=12cm}}
\end{center}
\end{figure}

Casiano Rodríguez León
2013-04-23