From 6ca84f1794af830ada9736d7289ce29aabb04ca3 Mon Sep 17 00:00:00 2001 From: t7yang Date: Mon, 10 Jan 2022 08:38:05 +0800 Subject: remove `notranslate` class in zh-TW --- .../reference/global_objects/proxy/index.html | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'files/zh-tw/web/javascript/reference/global_objects/proxy') diff --git a/files/zh-tw/web/javascript/reference/global_objects/proxy/index.html b/files/zh-tw/web/javascript/reference/global_objects/proxy/index.html index 54a71be888..f41b0a6caa 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/proxy/index.html +++ b/files/zh-tw/web/javascript/reference/global_objects/proxy/index.html @@ -27,7 +27,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Proxy

語法

-
var p = new Proxy(target, handler);
+
var p = new Proxy(target, handler);
 

參數

@@ -58,7 +58,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Proxy

In this simple example the number 37 gets returned as the default value when the property name is not in the object. It is using the get handler.

-
var handler = {
+
var handler = {
     get: function(target, name) {
         return name in target ?
             target[name] :
@@ -78,7 +78,7 @@ console.log('c' in p, p.c); // false, 37
 
 

In this example, we are using a native JavaScript object to which our proxy will forward all operations that are applied to it.

-
var target = {};
+
var target = {};
 var p = new Proxy(target, {});
 
 p.a = 37; // operation forwarded to the target
@@ -90,7 +90,7 @@ console.log(target.a); // 37. The operation has been properly forwarded
 
 

With a Proxy, you can easily validate the passed value for an object. This example uses the set handler.

-
let validator = {
+
let validator = {
   set: function(obj, prop, value) {
     if (prop === 'age') {
       if (!Number.isInteger(value)) {
@@ -120,7 +120,7 @@ person.age = 300; // Throws an exception

A function proxy could easily extend a constructor with a new constructor. This example uses the construct and apply handlers.

-
function extend(sup, base) {
+
function extend(sup, base) {
   var descriptor = Object.getOwnPropertyDescriptor(
     base.prototype, 'constructor'
   );
@@ -161,7 +161,7 @@ console.log(Peter.age);  // 13

Sometimes you want to toggle the attribute or class name of two different elements. Here's how using the set handler.

-
let view = new Proxy({
+
let view = new Proxy({
   selected: null
 },
 {
@@ -196,7 +196,7 @@ console.log(i2.getAttribute('aria-selected')); // 'true'

The products proxy object evaluates the passed value and convert it to an array if needed. The object also supports an extra property called latestBrowser both as a getter and a setter.

-
let products = new Proxy({
+
let products = new Proxy({
   browsers: ['Internet Explorer', 'Netscape']
 },
 {
@@ -241,7 +241,7 @@ console.log(products.latestBrowser); // 'Chrome'

This proxy extends an array with some utility features. As you see, you can flexibly "define" properties without using Object.defineProperties. This example can be adapted to find a table row by its cell. In that case, the target will be table.rows.

-
let products = new Proxy([
+
let products = new Proxy([
   { name: 'Firefox', type: 'browser' },
   { name: 'SeaMonkey', type: 'browser' },
   { name: 'Thunderbird', type: 'mailer' }
@@ -302,7 +302,7 @@ console.log(products.number); // 3
 
 

Now in order to create a complete sample traps list, for didactic purposes, we will try to proxify a non native object that is particularly suited to this type of operation: the docCookies global object created by the "little framework" published on the document.cookie page.

-
/*
+
/*
   var docCookies = ... get the "docCookies" object here:
   https://developer.mozilla.org/en-US/docs/DOM/document.cookie#A_little_framework.3A_a_complete_cookies_reader.2Fwriter_with_full_unicode_support
 */
-- 
cgit v1.2.3-54-g00ecf