why are there 2 quotes around Session.keys in chrome console - javascript

look at
Session.keys.selected_player
Is it even a valid String? Just wondering.

Seems like the values in the Session are stored as EJSON strings, so the string you're storing has been passed to the EJSON.stringify function, which returns a string containing your string. It may make more sense to you if try passing the object {test: "test"} to EJSON.stringify and study the return value.

It is a valid string.
Session is an instance of IdMap - Meteor's dictionary class which allows you to use strings, numbers and other EJSONable data as a key. It is not easy to do with plain JS objects as some strings are special properties (like __proto__ or prototype), numbers are converted to strings, etc.

Related

casting a string as a JavaScript object

In JavaScript, I can cast convert numbers to strings and vice versa, but there is no way to cast convert a string to an object
num = 1
str = '1'
num_as_str = String(num) // "1"
str_as_num = Number(str) // 1
str = '{ a: "foo", b: "bar", c: { a: "foo", b: "bar" }}'
str_as_obj = Object(str) // does not exist
Assuming my string (to be cast converted as an object) is predictable and relatively simply, what is the best way to achieve the above other than using a regexp to parse out the indiv key-val pairs? Suggestions welcome
background: I want to do the above because I want to be able to pass and receive complex values in a URL. For example, /index.html?q=within({r:20, u: "kms", lat: 35.32, lng: -22.0132}) (of course, I will URL encode/decode properly along the way). Fwiw, the node querystring module wipes out anything other strings, numbers, booleans and arrays by coercing them to empty strings.
background2: of course, I know about JSON.parse and JSON.stringify, but I have a user-submitted URL querystring param that is most easily transmitted as an object, except a querystring cannot deal with an object. That is what I am trying to find a way around.
I want to do the above because I want to be able to pass and receive complex values in a URL
Use JSON, not a JavaScript object initializer.
of course, I know about JSON.parse and JSON.stringify, but I have a user-submitted URL querystring param that is most easily transmitted as an object
The JSON version ({"a":"foo","b":"bar","c":{"a":"foo","b":"bar"}}) is just as easily transmitted. It URI-encodes to
%7B%22a%22%3A%22foo%22%2C%22b%22%3A%22bar%22%2C%22c%22%3A%7B%22a%22%3A%22foo%22%2C%22b%22%3A%22bar%22%7D%7D
vs. your original:
%7Ba%3A%22foo%22%2Cb%3A%22bar%22%2Cc%3A%7Ba%3A%22foo%22%2Cb%3A%22bar%22%7D%7D
Not much difference. (Yes, it's slightly longer.) And it has the advantage that JSON.parse is a built-in feature that doesn't allow arbitrary code execution.
If you must use the object literal string instead, you have to parse that string. There are two built-in ways to do it (eval and new Function), but unfortunately both of them execute the code, and don't limit what the code can be — you can't say, for instance, "only allow an object literal and no function calls."
It sounds like it's entirely possible that User A will be specifying the string and then you'll be evaluating it when showing a page to User B. If so, you can't use eval or new Function without exposing User B to risks from malicious code.
So you'll need to use a parser like Esprima or similar instead. You could probably also adapt Crockford's original JSON parser to allow unquoted property names.
So really, JSON is the way to go, but if you really don't want JSON, use a parser that doesn't allow arbitrary code execution.

Is Array Considered A Collection (Javascript)

I was looking for the answer to this and I could't find a very good answer but is arrays considered a collection in javascript.
I know that map is considered a collection but wasn't sure about Javascript
it's pretty basic really. It's reading the value in editTextNumber1 and storing it in a variable named edittextNumber1intvalue.
Fun fact about reading text values from controls in android: You can't just "getText(). If you do, you get an object that i believe is a stringbuilder. You have to use the .toString() method of the returned object.
Now on to part 2: Since the value returned is a string, you can't just force that into an integer. It has to be converted. Integer.parseInt will return an integer value if the string passed into it is numeric.
And that's it.

Difference in String conversions & Function calls - JavaScript

I was just wondering, in most of my projects I have been able to use:
the String() function,
the toString() method and
the JSON.stringify() method (I don't really use this),
to convert data in JavaScript to string without much difference and I was wondering what exactly the difference was between using each of them.
Thanks for reading through, I'll really appreciate your answer.
the String() function
This is the explicit use of the native constructor function that creates and returns a string object when used with the new operator or the string value only when used without new.
the toString() method
This invokes the object's toString() method which returns the string representation of the object, which, if not overridden, usually is something like [object Object], which indicates the instance and the type. Custom objects often will override this inherited method to be able to display the best string representation of that particular object.
JSON.stringify()
This takes an object and converts it into the JSON data format. Without using an optional "replacer" function, all properties that store functions will be stripped out of the string. This is generally used when an object is packaged to hold data and then that data is to be sent over HTTP to another location.
String() is a global string constructor, that accepts anything htat should be converted to string ad returns primitive string. Under the hood this calls Object.prototype.toString()(https://www.ecma-international.org/ecma-262/5.1/#sec-15.5.1.1)
Object.prototype.toString() is a method that return string representation of an object: the value of type String that is the result of concatenating the three Strings "[object ", class, and "]", where class is internal property of object. This function can be overriden
JSON.stringify() method converts a JavaScript value to a JSON string; Boolean, Number, and String objects are converted to the primitive values during the process

How does String.length work in JavaScript?

I want to know how is the string length of a string calculated in js.
Is is a function call or a class data member.
I want to know what happens when we execute the following code :
a = 'this is a string';
console.log(a.length); // what actually happens at this point?
Also if a do this :
a += ' added something';
console.log(a.length); // at what point is the new length calculated
//and/or updated for the object 'a';
And at last, do I need to store the string length in a temp variable while using a loop over the string or can I directly use the following (which one is faster/processor efficient) :
for(var i=0;i<a.length;i++){
// doing anything here
}
Summing up my question, I want to know the processing behind String.length and which practice is better while looping over strings?
A string is immutable in JavaScript.
a += "somestring" doesn't change the length of a string but makes a new string.
This means there is no "new length", but the length is just part of the definition of the string (more precisely it is stored in the same structure in implementations).
Regarding
for(i=0;i<a.length;i++){ // did you forget the 'var' keyword ?
a not so uncommon practice (if you don't change a) was to optimize it as
for (var i=0, l=a.length; i<l; i++)
in order to avoid the reading of the length but if you compare the performances with modern engines, you'll see this doesn't make the code any faster now.
What you must remember : querying the length of a string is fast because there is no computation. What's a little less fast is building strings (for example with concatenation).
Strings are a primitive type. At least that's what the documentation says. But we can access the length of the string as if we are accessing the property of an object(with the dot notation). Which indicates it's an object, Right?
Turns out, whenever we make a call from the string primitive to some property using the dot notation (for example, say length), the Js engine will take this primitive string and wrap it into an equivalent wrapper object, which is a String object. And then, the .length on that String object returns the length.
Interesting thing to note here is, that when we do something like this, our string still stays the same primitive string during all of this. And a temporary object is created to make our string operation work. Once the required property is fetched, this temporary object is deleted from the memory.
Hope this gives some high level understanding.
I'm answering your first question.
I'm also curious about this puzzle so I did some search myself, ended up finding -
Based on String documentation from Mozilla:
String literals (denoted by double or single quotes) and strings
returned from String calls in a non-constructor context (i.e., without
using the new keyword) are primitive strings. JavaScript automatically
converts primitives to String objects, so that it's possible to use
String object methods for primitive strings. In contexts where a
method is to be invoked on a primitive string or a property lookup
occurs, JavaScript will automatically wrap the string primitive and
call the method or perform the property lookup.
So as I understand, when you use somestring.length, the primitive string will first be wrapped as a String object, and then since the object has its property length, so it's just a internal method call to access and return.

How do I access or replicate string value of an object?

For example, I'm trying to isolate the first 5 characters of window.location.
var ltype, string = 'string';
console.log(window.location); // file:///C:/for example
console.log(typeof window.location); // [OBJECT]
lType=window.location.substr(0,5); // 'not a function' (quite so)
string=window.location;
lType=string.substr(0,5); // fails similarly
Q1: Can I somehow 'bind' substr() to window.location?
I can see that string=window.location replicates a reference and not
a value, so
Q2: How can a separate, discrete copy of a complex structure such as an object or an array be created [without using JSON.stringify() or JSON.parse() - which is what I am presently resorting to]?
try
string = window.location.href.toString();
instead of
string=window.location;
Because window.location will return object not a string.
window.location is an object, so you can't use string functions on it - as you've noticed. In order to get the actual location as a string (to perform string operations on it), you'll need to convert it to a string somehow.
window.location.href is a property provided by the object itself.
window.location.toString() is a method on all JavaScript objects, overridden here.
However, beware of the XY problem. It looks to me like you're trying to retrieve the protocol (the http: bit) of the URI. There's a property for that too - window.location.protocol.
lType = window.location.protocol;
You should use that - it's more robust (consider https:// or, worse, ftp://...).

Categories

Resources