Questions tagged [computed-properties]

Computed properties are functions declared as property. There are multiple languages supporting computed properties, so please use this tag in combination with tags like [swift], [javascript], etc.

Computed properties are functions declared as property. They usually don't store a specific value, but return a value dynamically. Computed properties can also be used to perform additional actions when reading or writing them.


Example in Swift:

var computedProperty: Int {
    get {
        return 1337
    }
    set (new) {
        print("The property was set to \(new)")
    }
}
572 questions
314
votes
14 answers

Reactjs setState() with a dynamic key name?

EDIT: this is a duplicate, see here I can't find any examples of using a dynamic key name when setting the state. This is what I want to do: inputChangeHandler : function (event) { this.setState( { event.target.id : event.target.value }…
trad
  • 7,581
  • 4
  • 20
  • 17
288
votes
12 answers

Methods vs Computed in Vue

What is the main difference between a method and a computed value in Vue.js? They seem the same and interchangeable to me.
Bootstrap4
  • 3,671
  • 4
  • 13
  • 17
192
votes
8 answers

Instance member cannot be used on type

I have the following class: class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count } } Compilation fails with the message: Instance member 'categoriesPerPage' cannot be used on…
Aderstedt
  • 6,301
  • 5
  • 28
  • 44
181
votes
5 answers

Vuex - Computed property "name" was assigned to but it has no setter

I have a component with some form validation. It is a multi step checkout form. The code below is for the first step. I'd like to validate that the user entered some text, store their name in the global state and then send then to the next step. I…
Connor Leech
  • 18,052
  • 30
  • 105
  • 150
100
votes
5 answers

VueJS How can I use computed property with v-for

How can I use computed property in lists. I am using VueJS v2.0.2. Here's the HTML:

{{fullName}}

Here's the Vue code: var items = [ { id:1, firstname:'John',…
Inoyatulloh
  • 1,513
  • 2
  • 15
  • 17
93
votes
4 answers

Use placeholders in YAML

Is there a way to use placeholders in YAML like this: foo: &FOO <>: type: number default: <> bar: - *FOO propname: "some_prop" default: "some default"
86
votes
4 answers

Difference between computed property and property set with closure

I'm new to Swift. What is the difference between a computed property and a property set to a closure? I know a computed property gets recalculated each time. Is it different for the closure? i.e. Closure: var pushBehavior: UIPushBehavior = { …
Lightsout
  • 3,454
  • 2
  • 36
  • 65
81
votes
3 answers

Watching computed properties

I have a component with the following hash { computed: { isUserID: { get: function(){ return this.userId? } } } Should I be watching isUserID or userId for changes? Can you watch computed properties?
Kendall
  • 5,065
  • 10
  • 45
  • 70
56
votes
2 answers

How to access a computed property from a method in a Single File Component with Vue.js

I have a normal single file component which has both a computed property and some methods: