In the Javascript collection, use the value from an earlier key / value at a later value

I know this sounds silly, but now my curiosity made me think about this idea. Is it possible to declare a collection of key / value pairs, for example var collection = {a:"1", b:"2"}, and then have a third pair sayingc: "3" + b.value?

+4
source share
5 answers

The first way using the property getter.

Note: cit is a property of both band c.

var collection = {
  a: 1,
  b: 2,
  get c() {
    return this.a + this.b;
  }
};

console.log(collection.a); // 1
console.log(collection.b); // 2
console.log(collection.c); // 3
Run codeHide result

The second way, using the compressed method in ES6 or more "classic" function declaration in ES5.

Note: cnow is a function.

let collection = {
  a: 1,
  b: 2,
  c() {
    return this.a + this.b;
  }
};

console.log(collection.a); // 1
console.log(collection.b); // 2
console.log(collection.c()); // 3 called as a function
Run codeHide result

, .

, , , , "".

var collection = {
  a: 1,
  b: 2,
  c: 3,
  init: function() {
    this.c = this.a + this.b;
  }
};
collection.init();
console.log(collection.a); // 1
console.log(collection.b); // 2
console.log(collection.c); // 3
Hide result

Forth way, c.

var collection = {
  a: 1,
  b: 2,
  c: undefined
};
collection.c = collection.a + collection.b;
console.log(collection.a); // 1
console.log(collection.b); // 2
console.log(collection.c); // 3
Hide result

, getter "3" b ( , ), :

var collection = {
  a: 1,
  b: 2,
  get c() {
    return '3' + this.b;
  }
};
console.log(collection.a); //1
console.log(collection.b); //2
console.log(collection.c); //"32"
console.log(typeof collection.c); // string
Hide result
+6

:

var collection = {
 a: 1,
 b: 2,
 c: a + b
}

:

var collection = {
 a: 1,
 b: 2
}

collection.c = collection.a + collection.b;
+3

, JavaScript:

var collection = {a:"1", b:"2"}
var newObj = {c: function(){
      return "3" + collection.b
    }
  }
console.log( newObj.c() );
Hide result
+2

c: "3" + bThe same .so object does not work. You need to insert the key and value into the .declare value object cfrom an external object

var collection = {
  a: "1",
  b: "2",
}
collection['c'] = '3'+collection.b

console.log(collection)
Run codeHide result
0
source

You cannot submit “links” that are rated like this. Expressions are evaluated before appointment.

let obj = {
    a: 1,
    b: 2
};

obj.c = 3 + obj.b;

console.log(obj.c);
> 5

obj.b = 10;
console.log(obj.c);
> 5

If you really wanted to do something like this, you can create a new type of object with a form of lazy evaluation.

function Lazy(value) {
  this.value = value;
}

Lazy.prototype.valueOf = function() {
  if (typeof this.value === 'function') {
    return this.value();
  }
  return this.value;
};

Lazy.prototype.toString = function() {
  return this.valueOf() + "";
}

/* Then you can do this.. */
let obj = {
  a: new Lazy(1),
  b: new Lazy(2)
};

/* We have to defer the evaluation of the expression. */
obj.c = new Lazy(() => 3 + obj.b);

console.log(obj.c.toString());

obj.b = 10;
console.log(obj.c.toString());
Run codeHide result
0
source

Source: https://habr.com/ru/post/1676159/


All Articles