Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Proxy and Reflect#451

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
joaquinelio merged 12 commits intojavascript-tutorial:masterfromjoaquinelio:leproso
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
12 commits
Select commitHold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions1-js/99-js-misc/01-proxy/01-error-nonexisting/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@ function wrap(target) {
if (prop in target) {
return Reflect.get(target, prop, receiver);
} else {
throw new ReferenceError(`Property doesn't exist: "${prop}"`)
throw new ReferenceError(`La propiedad no existe: "${prop}"`)
}
}
});
Expand All@@ -19,5 +19,5 @@ function wrap(target) {
user = wrap(user);

alert(user.name); // John
alert(user.age); // ReferenceError:Property doesn't exist: "age"
alert(user.age); // ReferenceError:La propiedad no existe: "age"
```
16 changes: 8 additions & 8 deletions1-js/99-js-misc/01-proxy/01-error-nonexisting/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
# Erroron reading non-existent property
# Erroral leer una propiedad no existente

Usually, an attempt to read a non-existent property returns `undefined`.
Usualmente, el intento de leer una propiedad que no existe devuelve `undefined`.

Create a proxy that throws an error for an attempt to read of a non-existent property instead.
Crea en su lugar un proxy que arroje un error por intentar leer una propiedad no existente.

That can help to detect programming mistakes early.
Esto puede ayudar a detectar equivocaciones en la programación en forma temprana.

Write a function `wrap(target)`that takes an object `target`and return a proxythat adds this functionality aspect.
Escribe una función `wrap(target)`que tome un objeto `target`y devuelva un proxyque agregue este aspecto de funcionalidad.

That's how it should work:
Así es como debe funcionar:

```js
let user = {
Expand All@@ -18,7 +18,7 @@ let user = {
function wrap(target) {
return new Proxy(target, {
*!*
/*your code */
/*tu código */
*/!*
});
}
Expand All@@ -27,6 +27,6 @@ user = wrap(user);

alert(user.name); // John
*!*
alert(user.age); // ReferenceError:Property doesn't exist: "age"
alert(user.age); // ReferenceError:La propiedad no existe: "age"
*/!*
```
4 changes: 2 additions & 2 deletions1-js/99-js-misc/01-proxy/02-array-negative/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,8 +5,8 @@ let array = [1, 2, 3];
array = new Proxy(array, {
get(target, prop, receiver) {
if (prop < 0) {
//even if we access it like arr[1]
// propis a string,so need to convert it to number
//incluso aunque la accedamos como arr[1]
// propes un string,así que necesitamos convertirla a number
prop = +prop + target.length;
}
return Reflect.get(target, prop, receiver);
Expand Down
22 changes: 11 additions & 11 deletions1-js/99-js-misc/01-proxy/02-array-negative/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@

#Accessing array[-1]
#Accediendo a array[-1]

In some programming languages, we can access array elements using negative indexes, counted from the end.
En algunos lenguajes de programación podemos acceder a los arrays usando índices negativos, contando desde el final.

Like this:
Como esto:

```js
let array = [1, 2, 3];

array[-1]; // 3,the last element
array[-2]; // 2,one step from the end
array[-3]; // 1,two steps from the end
array[-1]; // 3,el último elemento
array[-2]; // 2,el penúltimo elemento, uno antes del final
array[-3]; // 1,el antepenúltimo elemento, dos antes el final
```

In other words, `array[-N]`is the same as `array[array.length - N]`.
En otras palabras, `array[-N]`es lo mismo que `array[array.length - N]`.

Create a proxyto implement that behavior.
Crea un proxypara implementar tal comportamiento.

That's how it should work:
Así es como debe funcionar:

```js
let array = [1, 2, 3];

array = new Proxy(array, {
/*your code */
/*tu código */
});

alert( array[-1] ); // 3
alert( array[-2] ); // 2

//Other array functionality should be kept "as is"
//el resto de la funcionalidad debe mantenerse igual.
```
18 changes: 9 additions & 9 deletions1-js/99-js-misc/01-proxy/03-observable/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
The solution consists of two parts:
La solución consiste de dos partes:

1.Whenever `.observe(handler)`is called, we need to remember thehandler somewhere, to be able to call it later. We can store handlers right in the object, using our symbolas the property key.
2.We need aproxywith`set`trap to call handlers in case of any change.
1.Cuando `.observe(handler)`es llamado, necesitamos recordar el manejador 'handler' en algún lugar para poder llamarlo después. Podemos almacenar los manejadores directamente en el objeto, usando nuestro symbolcomo clave de la propiedad.
2.Necesitamos unproxycon la trampa`set`que llame a los manejadores en caso de cualquier cambio.

```js run
let handlers = Symbol('handlers');

function makeObservable(target) {
// 1.Initialize handlers store
// 1.Inicializa el almacén de manejadores
target[handlers] = [];

//Store the handler function inarrayfor future calls
//Almacena la función manejadora en elarraypara llamadas futuras
target.observe = function(handler) {
this[handlers].push(handler);
};

// 2.Create a proxyto handle changes
// 2.Crea un proxypara manejar cambios
return new Proxy(target, {
set(target, property, value, receiver) {
let success = Reflect.set(...arguments); //forward the operation to object
if (success) { //if there werenoerror while setting the property
//call all handlers
let success = Reflect.set(...arguments); //reenvía la operación al objeto
if (success) { //sinohay errores al establecer la propiedad
//llama a todos los manejadores
target[handlers].forEach(handler => handler(property, value));
}
return success;
Expand Down
14 changes: 7 additions & 7 deletions1-js/99-js-misc/01-proxy/03-observable/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@

# Observable

Create a function `makeObservable(target)`that "makes the object observable"by returning a proxy.
Crea una función `makeObservable(target)`que "haga el objeto observable"devolviendo un proxy.

Here's how it should work:
Así es como debe funcionar:

```js run
function makeObservable(target) {
/*your code */
/*tu código */
}

let user = {};
Expand All@@ -17,11 +17,11 @@ user.observe((key, value) => {
alert(`SET ${key}=${value}`);
});

user.name = "John"; //alerts: SET name=John
user.name = "John"; //alerta: SET name=John
```

In other words, an object returned by `makeObservable`is just like theoriginalone, but also has the method `observe(handler)`that sets`handler`function to be called on any property change.
En otras palabras, un objeto devuelto por `makeObservable`es como eloriginalpero que también tiene el método `observe(handler)`que establece una función`handler`que será llamada en cualquier cambio de propiedad.

Whenever a property changes, `handler(key, value)`is called with the name and value of the property.
cada vez que una propiedad cambie, `handler(key, value)`es llamado con el nombre y el valor de la propiedad.

P.S. In this task, please only take care about writing to a property. Other operations can be implemented in a similar way.
P.D. En esta tarea, solo toma en cuenta la escritura de una propiedad. Otras operaciones pueden ser implementadas de manera similar.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp