小编Wea*_*ter的帖子

JavaScript - 代理集与defineProperty

我想构建一个检测对象更改的代理:

  • 定义了新属性。
  • 现有属性已更改。

代码示例 1 -defineProperty

const me = {
  name: "Matt"
}

const proxy = new Proxy(me, {
  defineProperty: function(target, key, descriptor) {
    console.log(`Property ${key} defined.`);
    return Object.defineProperty(target, key, descriptor);
  }
});

proxy // { name: 'Matt' }

proxy.name = "Mark";
// Property name defined.
// Mark

proxy.age = 20;
// Property age defined.
// 20
Run Code Online (Sandbox Code Playgroud)

代码示例 1 - 观察

  • proxy有一个属性name,这是我所期望的。
  • 更改name属性告诉我name已定义;不是我所期望的。
  • 定义age属性告诉我age已经定义;正如我所料。

代码示例 2 - …

javascript proxy object object-properties

12
推荐指数
1
解决办法
1235
查看次数

Using function templatefile(path, vars) with a remote-exec provisioner

With terraform 0.12, there is a templatefile function but I haven't figured out the syntax for passing it a non-trivial map as the second argument and using the result to be executed remotely as the newly created instance's provisioning step.

Here's the gist of what I'm trying to do, although it doesn't parse properly because one can't just create a local variable within the resource block named scriptstr.

While I'm really trying to get the output of the templatefile call …

terraform

2
推荐指数
1
解决办法
3246
查看次数

特征的 impl 可以指定来自方法的输入参数的生命周期吗?

对于一个类型

pub struct Child<'a> {
    buf: &'a mut [u8],
}
Run Code Online (Sandbox Code Playgroud)

我可以定义一个特征并实现该类型的特征,但其生命周期绑定到调用函数的上下文(而不是本地循环上下文):

pub trait MakeMut<'a> {
    fn make_mut(buf: &'a mut [u8]) -> Self;
}

impl<'a> MakeMut<'a> for Child<'a> {
    fn make_mut(buf: &'a mut [u8]) -> Self {
        Self { buf }
    }
}
Run Code Online (Sandbox Code Playgroud)

首先展示一个有点有效的示例,因为x仅在循环上下文中借用,因为 Child::make_mut 是在函数中硬编码的map1

pub fn map1<F>(mut func: F)
    where
        F: FnMut(&mut Child),
{
    let mut vec = vec![0; 16];
    let x = &mut vec;
    for i in 0..2 {
        let offset = i * …
Run Code Online (Sandbox Code Playgroud)

traits lifetime rust lifetime-scoping

0
推荐指数
1
解决办法
460
查看次数