Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Decorators and forwarding, call/apply#171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
15 changes: 8 additions & 7 deletions1-js/06-advanced-functions/09-call-apply-decorators/01-spy-decorator/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
functionšpión(funkce) { | ||
functionobal(...argumenty) { | ||
//použijeme ...argumenty místoarguments, abychom do obal.volání uložili „skutečné“ pole | ||
obal.volání.push(argumenty); | ||
returnfunkce.apply(this,argumenty); | ||
} | ||
obal.volání = []; | ||
returnobal; | ||
} | ||
4 changes: 2 additions & 2 deletions1-js/06-advanced-functions/09-call-apply-decorators/01-spy-decorator/_js.view/source.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
functionšpión(funkce) { | ||
//váš kód | ||
} | ||
42 changes: 21 additions & 21 deletions1-js/06-advanced-functions/09-call-apply-decorators/01-spy-decorator/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,44 @@ | ||
describe("špión", function() { | ||
it("zapisuje volání do své vlastnosti", function() { | ||
functionpráce() {} | ||
práce =špión(práce); | ||
assert.deepEqual(práce.volání, []); | ||
práce(1, 2); | ||
assert.deepEqual(práce.volání, [ | ||
[1, 2] | ||
]); | ||
práce(3, 4); | ||
assert.deepEqual(práce.volání, [ | ||
[1, 2], | ||
[3, 4] | ||
]); | ||
}); | ||
it("transparentně obaluje funkce", function() { | ||
letsečti = sinon.spy((a, b) => a + b); | ||
letobalenéSečti =špión(sečti); | ||
assert.equal(obalenéSečti(1, 2), 3); | ||
assert(sečti.calledWith(1, 2)); | ||
}); | ||
it("transparentně obaluje metody", function() { | ||
letvypočítej = { | ||
sečti: sinon.spy((a, b) => a + b) | ||
}; | ||
vypočítej.obalenéSečti =špión(vypočítej.sečti); | ||
assert.equal(vypočítej.obalenéSečti(1, 2), 3); | ||
assert(vypočítej.sečti.calledWith(1, 2)); | ||
assert(vypočítej.sečti.calledOn(vypočítej)); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion1-js/06-advanced-functions/09-call-apply-decorators/01-spy-decorator/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Obal, který vrátí `špión(f)`, by si měl uložit všechny argumenty a pak pomocí`f.apply`přesměrovat volání. |
24 changes: 12 additions & 12 deletions1-js/06-advanced-functions/09-call-apply-decorators/01-spy-decorator/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion1-js/06-advanced-functions/09-call-apply-decorators/02-delay/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
functionzpozdi(f, ms) { | ||
return function() { | ||
setTimeout(() => f.apply(this, arguments), ms); | ||
46 changes: 23 additions & 23 deletions1-js/06-advanced-functions/09-call-apply-decorators/02-delay/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
describe("zpozdi", function() { | ||
before(function() { | ||
this.hodiny = sinon.useFakeTimers(); | ||
}); | ||
after(function() { | ||
this.hodiny.restore(); | ||
}); | ||
it("volá funkci až po specifikované době", function() { | ||
letzačátek = Date.now(); | ||
function f(x) { | ||
assert.equal(Date.now() -začátek, 1000); | ||
} | ||
f = sinon.spy(f); | ||
let f1000 =zpozdi(f, 1000); | ||
f1000("test"); | ||
this.hodiny.tick(2000); | ||
assert(f.calledOnce, 'ověřenícalledOnceselhalo'); | ||
}); | ||
it("předává argumenty a this", function() { | ||
letzačátek = Date.now(); | ||
letuživatel = { | ||
řekniAhoj: function(věta, kdo) { | ||
assert.equal(this,uživatel); | ||
assert.equal(věta, "Ahoj"); | ||
assert.equal(kdo, "Jan"); | ||
assert.equal(Date.now() -začátek, 1500); | ||
} | ||
}; | ||
uživatel.řekniAhoj = sinon.spy(uživatel.řekniAhoj); | ||
letšpión =uživatel.řekniAhoj; | ||
uživatel.řekniAhoj =zpozdi(uživatel.řekniAhoj, 1500); | ||
uživatel.řekniAhoj("Ahoj", "Jan"); | ||
this.hodiny.tick(2000); | ||
assert(špión.calledOnce, 'ověřenícalledOnceselhalo'); | ||
}); | ||
}); |
22 changes: 11 additions & 11 deletions1-js/06-advanced-functions/09-call-apply-decorators/02-delay/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions1-js/06-advanced-functions/09-call-apply-decorators/02-delay/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
functionvyčkej(funkce, ms) { | ||
letčasovač; | ||
return function() { | ||
clearTimeout(časovač); | ||
časovač = setTimeout(() =>funkce.apply(this, arguments), ms); | ||
}; | ||
} |
42 changes: 21 additions & 21 deletions1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
describe('vyčkej', function () { | ||
before(function () { | ||
this.hodiny = sinon.useFakeTimers(); | ||
}); | ||
after(function () { | ||
this.hodiny.restore(); | ||
}); | ||
it('pro jedno volání -spustí je po zadané době v ms', function () { | ||
const f = sinon.spy(); | ||
constvyčkáváno =vyčkej(f, 1000); | ||
vyčkáváno('test'); | ||
assert(f.notCalled, 'nevolá se okamžitě'); | ||
this.hodiny.tick(1000); | ||
assert(f.calledOnceWith('test'), 'voláno po 1000 ms'); | ||
}); | ||
it('pro 3volání -spustí poslední po zadané době v ms', function () { | ||
const f = sinon.spy(); | ||
constvyčkáváno =vyčkej(f, 1000); | ||
vyčkáváno('a'); | ||
setTimeout(() =>vyčkáváno('b'), 200); //ignorováno (příliš brzy) | ||
setTimeout(() =>vyčkáváno('c'), 500); //spustí se(1000 msuplynulo) | ||
this.hodiny.tick(1000); | ||
assert(f.notCalled, 'nevoláno po 1000 ms'); | ||
this.hodiny.tick(500); | ||
assert(f.calledOnceWith('c'), 'voláno po 1500 ms'); | ||
}); | ||
it('udržuje si kontext volání', function () { | ||
let obj = { | ||
f() { | ||
assert.equal(this, obj); | ||
}, | ||
}; | ||
obj.f =vyčkej(obj.f, 1000); | ||
obj.f('test'); | ||
this.hodiny.tick(5000); | ||
}); | ||
}); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.