Solucio funcio com a metode

De wikiserver
Dreceres ràpides: navegació, cerca
<html>
   <head>
      <meta charset = "utf-8">
      <title>QUnit basic example</title>
      <link rel = "stylesheet" href = "https://code.jquery.com/qunit/qunit-1.22.0.css">
      <script src = "https://code.jquery.com/qunit/qunit-1.22.0.js"></script>
   </head>
   
   <body>
      <div id = "qunit"></div>
      <div id = "qunit-fixture"></div> 
      
      <script>
        
/*Contingut del fitxer exercici5.js */
function unica(){return this;};

var replica = unica;


var objecte1 = {
 clon : unica
}

var objecte2 = {
 clon : unica
}


QUnit.test("Conjunt de Tests", function(assert) {

assert.equal(unica(),window,  "Funció Única");
assert.equal(replica(), window, "Funció Replica");
assert.equal(objecte1.clon(), window, "objecte1 es un window?");
assert.equal(objecte1.clon(), objecte1, "objecte1 es un objeto");
assert.equal(objecte2.clon(),objecte2, "Objecte2 es un objecto");
});



      </script>
   </body>
</html>



Exercici

var persona = {
    name: 'edu',
    twitter: 'eiximenis',
    twitterUrl: 'http://twitter.com/' + this.twitter
};
 
console.log(persona.twitterUrl);    //http://twitter.com/undefined


Exercici

// this == window
console.debug(this);

function test(){
	// this == window
console.debug(this);
}

test();


Exercici


var obj = {
	name: 'obj',
	run: function(){
		// this == obj
		this.value = 1;
		console.debug(this.name);
	}
};

obj.run();


Exercici

var obj = {
	name: 'obj',
	run: function(){
		this.value = 1;
		console.debug(this); // this == obj
		
		(function(){ // se crea un nuevo scope
			console.debug(this); // this == window
		})();
		
		function test(){ // se crea un nuevo scope
			console.debug(this); // this == window
		}
		
		test();
	}
};

obj.run();


Exercici

var obj = {
	name: 'obj',
	run: function(){
		this.value = 1;
		console.debug(this); // this == obj
		
		(function(){
			console.debug(this); // this == obj
		}).call(this); // se autoejecuta con el método call
		
		this.test = function(){ // se define como método de obj
			console.debug(this); // this == obj
		}
		
		this.test(); // se ejecuta dentro del contexto de obj
	}
};

obj.run();