BranceLee / ES6-NoteBook

I am learn ES6 as a fresh man ,there are some notes when I am learning it
0 stars 0 forks source link

数组操作 #2

Open BranceLee opened 6 years ago

BranceLee commented 6 years ago

- [ 拦截读取属性的例子 ]

表示方式:a=new Proxy( target, handler )

其中target表示要拦截的对象,handler表示拦截的行为

let proxy=new Proxy( {} ,{ get: function(target, property){
return 35 } }) proxy.name //35 proxy.age //35

target:为目标对象,此处为空对象{}, 因proxy.name 执行的行为是访问proxy的name 属性,被代理商监听拦截get()获取target对象的property的属性读取,再返回35给你

- [ Proxy 实例与原型]

let proxy=new Proxy( {}, { get:(target,property)=>{ return 35 } })

const a=Object.create( proxy ) a.name

当 a.name 时,a对象本身没有name 属性,就会继续在其原型找,于是乎以proxy为对象原型里找有无name属性,此行为会触发代理功能,于是乎返回35.

1. - Proxy 支持的拦截操作一览,一共 13 种

get(target, propKey, receiver):拦截对象属性的读取,比如proxy.foo和proxy['foo']。 set(target, propKey, value, receiver):拦截对象属性的设置,比如proxy.foo = v或proxy['foo'] = v,返回一个布尔值。 has(target, propKey):拦截propKey in proxy的操作,返回一个布尔值。 deleteProperty(target, propKey):拦截delete proxy[propKey]的操作,返回一个布尔值。 ownKeys(target):拦截Object.getOwnPropertyNames(proxy)、Object.getOwnPropertySymbols(proxy)、Object.keys(proxy)、for...in循环,返回一个数组。该方法返回目标对象所有自身的属性的属性名,而Object.keys()的返回结果仅包括目标对象自身的可遍历属性。 getOwnPropertyDescriptor(target, propKey):拦截Object.getOwnPropertyDescriptor(proxy, propKey),返回属性的描述对象。 defineProperty(target, propKey, propDesc):拦截Object.defineProperty(proxy, propKey, propDesc)、Object.defineProperties(proxy, propDescs),返回一个布尔值。 preventExtensions(target):拦截Object.preventExtensions(proxy),返回一个布尔值。 getPrototypeOf(target):拦截Object.getPrototypeOf(proxy),返回一个对象。 isExtensible(target):拦截Object.isExtensible(proxy),返回一个布尔值。 setPrototypeOf(target, proto):拦截Object.setPrototypeOf(proxy, proto),返回一个布尔值。如果目标对象是函数,那么还有两种额外操作可以拦截。 apply(target, object, args):拦截 Proxy 实例作为函数调用的操作,比如proxy(...args)、proxy.call(object, ...args)、proxy.apply(...)。 construct(target, args):拦截 Proxy 实例作为构造函数调用的操作,比如new proxy(...args)

BranceLee commented 6 years ago

常用的字符串,数组转换

const string = "hello world";
string.split("");  // ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
string.split(" ");   // ["hello", "world"]

例二: const a="Matter" 不管用的 name[0]="m" console.log(a) //"Matter"

BranceLee commented 6 years ago

JS对象的操作

  1. Write the command to add the language "Go" to the end of the languages array.
  2. Change the difficulty to the value of 7.
  3. Using the delete keyword, write the command to remove the jokes key from the programming object.
  4. Write the command to add a new key called isFun and a value of true to the programming object.
  5. Using a loop, iterate through the languages array and console.log all of the languages.
  6. Using a loop, console.log all of the keys in the programming object.
  7. Using a loop, console.log all of the values in the programming object.
    
    var programming = {
    languages: ["JavaScript", "Python", "Ruby"],
    isChallenging: true,
    isRewarding: true,
    difficulty: 8,
    jokes: "http://stackoverflow.com/questions/234075/what-is-your-best-programmer-joke"
    }

// 1

programming.languages.push("Go");

// 2

programming.difficulty = 7;

// 3

delete programming.jokes;

// 4

programming.isFun = true;

// 5

for (var i = 0; i < programming.languages.length; i++) { console.log(programming.languages[i]); }

// 6

for (var key in programming){ console.log(key); }

// 7

for (var key in programming){ console.log(programming[key]); }