What is the meaning of this note in the spec? [duplicate] - ecmascript-6

This question already has answers here:
I read in the EcmaScript specification that certain methods are "generic". What does this mean?
(2 answers)
Closed 7 years ago.
What is the meaning of this? This is regarding the Array.prototype.concat method, but it's stated throughout the spec, again and again, for a series of methods.
The concat function is intentionally generic; it does not require that
its this value be an Array object. Therefore it can be transferred to
other kinds of objects for use as a method. Whether the concat
function can be applied successfully to a host object is
implementation-dependent.
Can you explain this in simple wording, perhaps providing an example as well?

It means that you can apply the method to an array-like object even if it's not an Array. These include NodeLists and the arguments object.
I sometimes use this feature to turn the arguments object into an array: [].slice.call(arguments) returns the argument list as an Array object, giving me native access to all the array methods.
You can read about this on MDN.

Related

Why ! after String in graphql? [duplicate]

This question already has answers here:
What is an exclamation point in GraphQL?
(3 answers)
Closed 2 years ago.
What is the purpose of appending ! after String param in graphql methods.
query method($param: String!)
This modifier means that the type cannot be null.
String! means that the field is non-nullable, meaning that the GraphQL service promises to always give you a value when you query this field. In the type language, we'll represent those with an exclamation mark
The Non-Null type modifier can also be used when defining arguments for a field, which will cause the GraphQL server to return a validation error if a null value is passed as that argument, whether in the GraphQL string or in the variables.
https://graphql.org/learn/schema/#type-system
What is an exclamation point in GraphQL?

Pass String with Json data to Map in Golang [duplicate]

This question already has answers here:
Golang parse JSON array into data structure
(3 answers)
Closed 5 years ago.
Currently I have stored in my database json objects as string. I want to pass them to a map to be able to consult any field as:
Mymap["Name"]
Mymap["Age"]
..
Let's say that my string would be something like:
'{"Name":["zero"],"Age":"10"}'
I don't know the structure of the data, so that Json can have many fields as required and also many levels of nestings (but I am worried more about to get at least the first level)
If you're dealing with a json object of arbitrary structure you can use a map of interfaces as the type to unmarshal it into.
map[string]interface{}
The encoding/json package will nicely unmarshal the json object into it, nested or not.
This, while very flexible, has an obvious downside, the types of the map's values are unknown and so to do anything useful with them you'll have to use a type assertion or type switch.
v, ok := m["key"].(Type)
https://play.golang.org/p/wM0gkU1g5G

Defining 'N' Number of Dynamic Query Parameters in Swagger [duplicate]

This question already has answers here:
How to document dynamic query parameter names in OpenAPI (Swagger)?
(2 answers)
Closed 3 years ago.
I have the scenario where an endpoint can accept 'N' number of query parameters with a non-predetermined name and value i.e. I have no idea what query parameters someone might pass in e.g.
?i=can&pass=anything&in=here
How would I describe the above using the Swagger specification?
Looks like it's not supported in the version 2.0 of the OpenAPI specification, but it may be addressed in future versions.
This issue and this pull request can give you more details.

Implicitly define Dictionary pairs [duplicate]

This question already has an answer here:
Flex dictionary literal
(1 answer)
Closed 8 years ago.
Background
Defining an Array is typically demonstrated as var array:Array = new Array(), however, this relegates array assignment to methods like array.push(value) or linear declarations like ...
array[0] = "apple"
array[1] = "orange"
Obviously, a more succinct format is an implicit declaration, where a double bracket is understood to define an array, and the index is automatically handled.
var array:Array = ["apple", "orange"];
The same works for Objects...
var obj:Object = {
"apple":"fritter",
"orange":"pie"
}
The Problem
The problem arises when trying to define a Dictionary's key:value pairs implicitly. Reading the documentation, I was shocked to only find one method on the class. The fact that it extends Object at least means for ... in are available, but that's about where the conveniences end.
Especially since we'll want to use weak keys, the one argument available to Dictionaries would need to be set to true, thereby precluding any kind of implicit definition. The same documentation outlines typical usage in the former (lengthier) format I demonstrated with arrays:
var dict:Dictionary = new Dictionary(true);
dict[key] = "Letters";
That's just not going to fly for complicated structures.
[ redacted with argument ]
Because the docs for both Array & Object never actually explain implicit declarations, I can't help but imagine there might be a way to do so with Dictionaries. Anyone know?
I'm thinking now my only option is to come up with some kind of method which maps a complex object tree to a dictionary... which is dumb, since it'd be faster to just use the long method first demonstrated.
First of all, even if you write the code with Object and don't quote up strings as in original question, you'll not receive the expected results. And if you are attempting to use an object as a key in with statement, the compiler will be confused whether you want dict[obj] or just obj to be assigned a value. So, if you are adding a property to an Object or Dictionary, use the brackets syntax:
obj["apple"]="fritter";
dict[obj]="bar";
Etc.

Is a single string value considered valid JSON? [duplicate]

This question already has answers here:
What is the minimum valid JSON?
(8 answers)
Closed 7 years ago.
Do you consider the JSON web response:
"A serialization error occurred"
to be valid or not?
Some validators accept it while others don't.
As for new JSON RFC, json, containing only single value is pretty valid.
A JSON text is a serialized value. Note that certain previous specifications of JSON constrained a JSON text to be an object or an array.
There's a change of heart on this between RFC4627 and RFC7159:
RFC4627:
A JSON text is a serialized object or array.
JSON-text = object / array
RFC7159:
A JSON text is a serialized value. Note that certain previous
specifications of JSON constrained a JSON text to be an object or an
array. Implementations that generate only objects or arrays where a
JSON text is called for will be interoperable in the sense that all
implementations will accept these as conforming JSON texts.
JSON-text = ws value ws
No philosophical or practical justification is provided for this change of heart. The earlier version probably makes more sense as it consistently dictates that both a singe list element and a single map element (a pair or tuple) be contained. The second version allows only a single list element to be uncontained.
According to the grammar exposed in http://www.json.org/ (which references the Standard ECMA-262 3rd Edition - December 1999 par.5.1.5 The JSON Grammar) it's wrong:
The initial element must be:
and then a value can be a string:
From RFC4627:
A JSON text is a serialized object or array.
JSON-text = object / array
IE, the root element has to be an object or array, and can't be a string value by itself.
I don't care if some validator accepts it. It's wrong. It's a question of good practice, Json format must be {"key": "value", .....}. If you consider that text Json, can work, but for the rest of programmer it's not a serious Json. If you use only that text, then you don't need Json.