0

i create new property name styleTitle and i want get styleTitle = title + styleTitle. i tried in vue js it show result undefineObject

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  title: "Style",
  styleTitle: this.title + "Object",
  eyeColor: "blue"
};
zark
  • 1
  • 1
  • It doesn't make sense to ask the question for both JS and Vue. The principle is the same but the actual solution depends on your case. It would be different for reactive Vue object. Consider reasking with your exact case if the dupe question doesn't solve the problem – Estus Flask Apr 07 '23 at 08:29
  • i try to use const person = { firstName: "John", lastName: "Doe", age: 50, title: "Style", styleTitle: funtion(){ this.title + "Object"} eyeColor: "blue" }; in vue js but out put : (funtion(){this.title + "Object"}) is text ? – zark Apr 07 '23 at 09:05
  • It would be done *in JS* the way that the answer suggested, but in Vue it usually isn't because this doesn't work with reactivity – Estus Flask Apr 07 '23 at 09:15
  • thank you @EstusFlask i'll try more . – zark Apr 07 '23 at 09:16
  • Hello @EstusFlask can u help show me some example about this [link](https://vuejs.org/guide/essentials/template-syntax.html#using-javascript-expressions) `
    `
    – zark Apr 07 '23 at 09:21
  • I'm not sure what you mean. If you have a specific case in mind, consider reasking the question that reflects your problem, it needs https://stackoverflow.com/help/mcve – Estus Flask Apr 07 '23 at 09:31

1 Answers1

0

If a property of an object is based on another property, consider using getters:

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  title: "Style",
  eyeColor: "blue",
  get styleTitle() {
    return this.title + "Object"
  },
};

console.log(person.styleTitle);
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
  • what get keyword meaning ? – zark Apr 07 '23 at 07:34
  • I attached the link to [`get` keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get). It should work for all major browsers, unless the object itself got serialized. – Hao Wu Apr 07 '23 at 07:37