Diferència entre revisions de la pàgina «Solucio funcio com a metode»
De wikiserver
(Es crea la pàgina amb «<pre> function unica(){return this;}; assert(unica() === window, "unica a la finestra"); var replica = unica; assert(replica() === window, "replicant a la finestra"); …».) |
|||
| (4 revisions intermèdies per 2 usuaris que no es mostren) | |||
| Línia 1: | Línia 1: | ||
| − | < | + | <source lang="html"> |
| + | <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;}; | function unica(){return this;}; | ||
| − | |||
| − | |||
var replica = unica; | var replica = unica; | ||
| − | |||
var objecte1 = { | var objecte1 = { | ||
| − | clon = unica | + | clon : unica |
| + | } | ||
| + | |||
| + | var objecte2 = { | ||
| + | clon : unica | ||
} | } | ||
| − | |||
| − | var | + | 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> | ||
| + | |||
| + | |||
| + | </source> | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | Exercici | ||
| + | |||
| + | <source lang="java"> | ||
| + | var persona = { | ||
| + | name: 'edu', | ||
| + | twitter: 'eiximenis', | ||
| + | twitterUrl: 'http://twitter.com/' + this.twitter | ||
| + | }; | ||
| + | |||
| + | console.log(persona.twitterUrl); //http://twitter.com/undefined | ||
| + | |||
| + | </source> | ||
| + | |||
| + | |||
| + | Exercici | ||
| + | |||
| + | <source lang="java"> | ||
| + | // this == window | ||
| + | console.debug(this); | ||
| + | |||
| + | function test(){ | ||
| + | // this == window | ||
| + | console.debug(this); | ||
} | } | ||
| − | + | test(); | |
| + | </source> | ||
| + | |||
| + | |||
| + | Exercici | ||
| + | |||
| + | |||
| + | <source lang="java"> | ||
| + | var obj = { | ||
| + | name: 'obj', | ||
| + | run: function(){ | ||
| + | // this == obj | ||
| + | this.value = 1; | ||
| + | console.debug(this.name); | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | obj.run(); | ||
| + | </source> | ||
| + | |||
| + | |||
| + | Exercici | ||
| + | |||
| + | <source lang="java"> | ||
| + | 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(); | ||
| + | </source> | ||
| + | |||
| + | |||
| + | Exercici | ||
| + | |||
| + | <source lang="java"> | ||
| + | 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(); |
| + | </source> | ||
Revisió de 17:16, 5 nov 2018
<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();