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#369

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

Open
polycaman wants to merge3 commits intojavascript-tutorial:master
base:master
Choose a base branch
Loading
frompolycaman:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
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(`Özellik yok: "${prop}"`)
}
}
});
Expand All@@ -19,5 +19,5 @@ function wrap(target) {
user = wrap(user);

alert(user.name); // John
alert(user.age); //Error: Property doesn't exist
alert(user.age); //Hata: Özellik yok
```
15 changes: 7 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,13 +1,12 @@

#Error on reading non-existant property
#Mevcut olmayan özelliği okuma hatası

Create a proxy that throws an error for an attempt to read of a non-existant property.
Mevcut olmayan bir özelliği okumaya çalışıldığında hata fırlatan bir proxy oluşturun.

That can help to detect programming mistakes early.
Bu, programlama hatalarını erken tespit etmeye yardımcı olabilir.

Write a function `wrap(target)` that takes an object `target` and return a proxy instead with that functionality.

That's how it should work:
Bir nesne `target` alan ve bu işlevselliğe sahip bir proxy döndüren `wrap(target)` fonksiyonunu yazın.
Şöyle çalışmalı:

```js
let user = {
Expand All@@ -17,7 +16,7 @@ let user = {
function wrap(target) {
return new Proxy(target, {
*!*
/*your code */
/*kodunuz */
*/!*
});
}
Expand All@@ -26,6 +25,6 @@ user = wrap(user);

alert(user.name); // John
*!*
alert(user.age); //Error: Property doesn't exist
alert(user.age); //Hata: Özellik yok
*/!*
```
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 likearr[1]
// propis astring, so need to convert it tonumber
//bunaarr[1] gibi erişsek bile
// propbir yazıdır(string), o zaman onu bir numaraya(number) çevirmeliyiz
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 @@

#Accessingarray[-1]
# array[-1]'e Erişmek

In some languages, we can access array elements using negative indexes, counted from the end.
Bazı dillerde, dizi elemanlarına sondan sayılarak negatif indekslerle erişebiliriz.

Like this:
Şöyle:

```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,son eleman
array[-2]; // 2,sondan bir önceki eleman
array[-3]; // 1,sondan iki önceki eleman
```

In other words, `array[-N]`is the same as`array[array.length - N]`.
Başka bir deyişle, `array[-N]`ifadesi`array[array.length - N]` ile aynıdır.

Create a proxy to implement that behavior.
Bu davranışı uygulamak için bir proxy oluşturun.

That's how it should work:
Şöyle çalışmalı:

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

array = new Proxy(array, {
/*your code */
/*kodunuz */
});

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

//Otherarray functionality should be kept "as is"
//Geri kalan dizi(array) özelliği "olduğu gibi" kalmalıdır
```
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:
Çözüm iki bölümden oluşur:

1.Whenever`.observe(handler)`is called, we need to remember thehandler somewhere, to be able to call it later. We can store it right in the object, using our symbol as the key.
2.We need a proxy with`set`trap to call handlers in case of any change.
1. `.observe(handler)`çağrıldığında,handler'ı daha sonra çağırabilmek için bir yerde saklamamız gerekir. Bunu, sembolümüzü anahtar olarak kullanarak doğrudan nesnede saklayabiliriz.
2.Herhangi bir değişiklik durumunda handler'ları çağırmak için`set`tuzağına sahip bir proxy'ye ihtiyacımız var.

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

function makeObservable(target) {
// 1.Initialize handlers store
// 1.Handler'ları saklamak için alanı başlat
target[handlers] = [];

//Store the handler function in array for future calls
//Handler fonksiyonunu ileride çağırmak için diziye ekle
target.observe = function(handler) {
this[handlers].push(handler);
};

// 2.Create a proxy to handle changes
// 2.Değişiklikleri yakalamak için bir proxy oluştur
return new Proxy(target, {
set(target, property, value, receiver) {
let success = Reflect.set(...arguments); //forward the operation to object
if (success) { //if there were no error while setting the property
//call all handlers
let success = Reflect.set(...arguments); //İşlemi nesneye ilet
if (success) { //Özellik atanırken hata yoksa
//Tüm handler'ları çağır
target[handlers].forEach(handler => handler(property, value));
}
return success;
Expand Down
17 changes: 8 additions & 9 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
#Gözlemlenebilir (Observable)

Create a function`makeObservable(target)`that "makes the object observable" by returning a proxy.
Bir nesneyi "gözlemlenebilir" yapan ve bir proxy döndüren`makeObservable(target)`fonksiyonunu oluşturun.

Here's how it should work:
Şöyle çalışmalı:

```js run
function makeObservable(target) {
/*your code */
/*kodunuz */
}

let user = {};
Expand All@@ -20,10 +20,9 @@ user.observe((key, value) => {
user.name = "John"; // alerts: SET name=John
```

In other words, an object returned by`makeObservable`has the method `observe(handler)`.
Başka bir deyişle,`makeObservable`tarafından döndürülen nesnede `observe(handler)` metodu bulunur.

Whenever a property changes,`handler(key, value)`is called with the name and value o the property.
Bir özelliğin değeri değiştiğinde, ilgili özelliğin adı ve değeri ile`handler(key, value)`çağrılır.


P.S. In this task, please handle only writing to a property. Other operations can be implemented in a similar way.
P.P.S. You might want to introduce a global variable or a global structure to store handlers. That's fine here. In real life, such function lives in a module, that has its own global scope.
Not: Bu görevde yalnızca bir özelliğe değer atamayı (yazmayı) ele alın. Diğer işlemler benzer şekilde uygulanabilir.
Ek Not: Handler'ları saklamak için global bir değişken veya global bir yapı kullanabilirsiniz. Burada bu uygundur. Gerçek hayatta, böyle bir fonksiyon kendi global kapsamına sahip bir modülde yaşar.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp