I'm trying to call a child component's method from my parent component using $refs. It works if I do it the regular way: this.$refs.actualNameOfTheChildComponent.someMethod()
But for my app's needs I have to use a variable name instead of the child component's actual name:
const previousAction = `action${i-1}`
this.$refs.previousAction.showConflict()
In the second line, Vue doesn't understand that I'm not referring to a child component named previousAction, but I'm referring to a child component whose name is the value of the variable previousAction.
What's the right way to do it?
Writing .something is just syntactic sugar for ["something"]. Therefore you can use:
const previousAction = `action${i-1}`
this.$refs[previousAction].showConflict()
You can read more about the differences between dot notation and bracket notation here.
There are some important differences between dot and bracket
notation:
Dot notation:
Property identifies can only be alphanumeric
(and _ and $)
Property identifiers cannot start with a number.
Property identifiers cannot contain variables.
OK — obj.prop_1,
obj.prop$
Not OK — obj.1prop, obj.prop name
Bracket notation:
Property identifiers have to be a String or a variable that references a String.
It is okay to use variables, spaces, and Strings that start
with numbers
OK — obj["1prop"], obj["prop name"]
Related
According to the Kotlin docs on destructuring declarations, the declared components should match the number of components on the right side:
Anything can be on the right-hand side of a destructuring declaration, as long as the required number of component functions can be called on it.
However, I discovered that this works even if the left hand side doesn't have the same number of components as on the right side of the assignment statement.
fun main() {
val (firstOnly) = Pair("key", "value")
println("firstOnly=${firstOnly}")
}
Is this legal Kotlin or is this is a bug? If it's legal, is there a reference?
If it's legal, is there a reference?
The Kotlin Language Specification says:
A special case of definition by convention is the destructuring declaration of properties [...]
This convention allows to introduce a number (one or more) of properties in the place of one by immediately destructuring the property during construction.
It says "one or more", so yes, declaring a single property by destructuring is allowed.
Also note that "required number of component functions can be called on it" doesn't mean the number of component functions has to equal to number of properties being declared. Let's put it this way: if I have 2 apples, and 1 apple is required. Do I have the "required number of apples"? Clearly the answer is yes.
If you still find it unclear, I think the spec says it better:
For each identifier the corresponding operator function componentK with
K being equal to the position of the placeholder in the declaration (starting from 1) is called without arguments.
which implies that those functions calls need to be valid. Whether or not other component functions exist is not relevant.
From the document you linked to:
Anything can be on the right-hand side of a destructuring declaration, as long as the required number of component functions can be called on it.
It doesn't say it has to match the number of variables declared on the left, only that it has the required number.
In fact, this is very useful when destructuring a List:
val (first, second) = listOf(1, 2, 3, 4, 5)
The documentation for List<T>.component1():
Throws an IndexOutOfBoundsException if the size of this list is less than 1.
Again, it doesn't restrict the list to being of size 1.
In vue 1 it was possible to do this:
<app v-ref:test-app></app>
and then reference it like so:
vm.$refs.testApp;
however in vue 2 the syntax for the ref has changed to:
<app ref="test-app"></app>
but this no longer can be referenced by
vm.$refs.testApp
instead it only works if:
<app ref="testApp"></app>
which in standard DOM stuff isn't allowed. Is it a bug? can kebab case no longer be used?
Since the syntax has been changed from that of a namespaced element attribute (i.e., v-ref:foo-bar) to a normal key-value-pair attribute (i.e., ref="fooBar"), the implicit kebab-case-to-camel-case conversion is no longer applicable because the reference name is a plain string and is not constrained by having to conform to the requisite lowercase-kebab-case HTML syntax.
In other words, you can identify a ref with any string, so it wouldn't make sense for Vue to manipulate it. Have a look at this CodePen for an example in action of what I mean.
But, basically, a plain string ref value means you can define a reference like this:
<div id="app" ref="test ** app!"></div>
and reference it from Vue like this:
this.$refs['test ** app!']
In short, no, it's not a bug but no, automatic kebab-case conversion no longer takes place.
Can someone please fill in the blanks for me, including a brief description of use and perhaps a code snippet? I'm well aware of the top two in particular, but a little hazy on the last one especially:
() - Used for calling a function, object instantiation, passing parameters, etc.
{} - Used for defining and adding elements to arrays or sets.
[] - Used for forcing an object to be treated as a type rather than keyword.
<> - Used for... ?
For Example, I see stuff like this all the time, but still not quite sure what the brackets means...
<TemplateContainer(GetType(TemplateItem))> _
Public Property MessageTemplate As ITemplate
VB.net uses parentheses for, among other things, arithmetic groupings and function parameters (both of which use parentheses in C#), as well as array subscripts and default-property parameters (both of which use brackets in C#), (indexers), etc. It also uses (Of ... ) to enclose a list of types (which would be enclosed in < ... > in C#, with no "Of" keyword.
Braces are used for array or set initialization expressions, and are also used when defining a generic type with multiple constraints (e.g. (Of Foo As {IEnumerable, IDisposable, Class})). Note that the latter usage is only permitted in constraints; it is alas not possible to e.g. Dim MyThing As {IEnumerable, IDisposable, Class}).
Braces are now also used for the New With {} construct:
Dim p = New Person With {.Name = "John Smith", .Age = 27}
Dim anon = New With {.Name = "Jack Smythe", .Age = 23}
Square brackets are used to enclose identifiers whose spelling would match that of a reserved word. For example, if a class defined a method called Not (perhaps the class was written in a language without a keyword Not), one could use such a method within VB by enclosing its name in square brackets (e.g. someVariable = [Not](5)). In the absence of the square brackets, the above expression would set someVariable to -6 (the result of applying the vb.net Not operator to the value 5).
Angle brackets, as noted elsewhere, are used for attributes. Note that in many cases, attributes are placed on the line above the thing they affect (so as to avoid pushing the affected variable past the right edge of the screen). In older versions of vb, such usage requires the use of a line-continuation mark (trailing underscore).
Angle brackets are also used for XML Literals and XML Axis Properties:
Dim xml = <simpleTag><anotherTag>text</anotherTag></simpleTag>
Console.WriteLine(xml.<anotherTag>.First.Value)
In this case it's used for the Attribute declaration. It can also be used in XML Literals as follows:
<TestMethod>
Public Sub ThisIsATest()
If 1 <> 0 Then
Dim foo = <root>
<child>this is some XML</child>
</root>
End If
End Sub
In VB.Net, <> is used to enclose Attributes.
VB.NET uses <> for attributes as well as to indicate "does not equal" (!=)
In your example it is just enclosing attributes. That same code in C# would be
[TemplateContainer(GetType(TemplateItem))]
public ITemplate MessageTemplate { get; set; }
This attribute is used in developing templated controls, which separate data from presentation. In other words, a templated control can retain the same functionality while changing it's appearance.
What is the meaning of the square brackets around the name of a property in the definition ?
Example :
Public Property [Date] As String
To use reserved keywords as identifiers, the brackets must be used to
distinguish between the identifier and the keyword:
dim [String] As String
public sub [Stop]
end sub
On msdn it says:
Any program element — such as a variable, class, or member — can have
the same name as a restricted keyword. For example, you can create a
variable named Loop. However, to refer to your version of it — which
has the same name as the restricted Loop keyword — you must either
qualify it by preceding it with its full namespace, or enclose it in
square brackets ([ ]), as in the following examples:
Reference here
This syntax allows you to use a reserved word as the name of a member or variable. Not recommended though IMHO from a code maintainability point of view (though see comments below for an alternative point of view on this particular point)!
Particularly not recommended if you're going to declare a property called "Date" as a string, but that's a separate issue...
Date is a reserved keyword in VB.NET, but can be used as a property or variable name if enclosed in square brackets:
http://msdn.microsoft.com/en-us/library/ksh7h19t(v=vs.90).aspx
In programming (and math) there are variables and constants. Is there a name to describe both of them?
I was thinking value, but that's not it. A value is what variables/constants contain, not what they are.
I would call it a symbol. From google:
sym·bol/ˈsimbəl/Noun
1. A thing that represents or stands for something else,
esp. a material object representing something abstract.
...
From what I know Its called a field
How about:
maths and logic: term
programming: l-value and r-value.
There are a few different terms I use, depending on context. I'll give you a list of the terms I (might) use - sometimes I'll just default to calling everything 'variables'.
Field - a variable or constant that's declared as part of the class definition.
Parameter - one of the inputs specified when defining a method in a class.
Argument - the actual value that you provide for a parameter when calling a method.
Method variable - a variable declared inside a method.
Method constant - a constant declared inside a method.
In OOP, the attribute can be both a variable and a constant.
Identifiers
In computer languages, identifiers are tokens (also called symbols) which name language entities. Some of the kinds of entities an identifier might denote include variables, types, labels, subroutines, and packages.
Symbols are super set of Identifiers
https://en.wikipedia.org/wiki/Identifier#In_computer_languages
How about "data item"?
One definition: https://www.yourdictionary.com/data-item
Example showing it can be used for local variables/constants as well (unlike "field" or "attribute"): https://www.microfocus.com/documentation/visual-cobol/VC222/EclWin/GUID-A3B817EE-1D63-4F67-A62C-61DE681C6719.html