I wonder how JS engine handles variable declaration in ES6 like:
No matter:
let V1 = "let Hello";
const V1 = "const Hello";
V1 = "reassigned Hello"
Or:
const V1 = "const Hello";
let V1 = "let Hello";
V1 = "reassigned Hello"
V1 is always "reassigned Hello"
I thought if a variable declared with const, then it should not be reassign value. Why it works?
You mentioned in the comments that you were following the React tutorial and using their CodePen example.
This Pen is setup to use Babel which transpiles the ES6 code into ES5, so the reason your assignment works is because this:
let V1 = "let Hello";
const V1 = "const Hello";
Is actually transpiled and executed as this:
var V1 = "let Hello";
var V1 = "const Hello";
If you disable Babel for the Pen and use a browser that supports const natively (i.e. any modern browser) you’ll see an exception.
If you test this in Chrome for example you will get
Uncaught SyntaxError: Identifier 'V1' has already been declared
Print
Related
The code above works however I would like to load the searchStrings array from a JSON file.
My goal is to have the json file on a shared drive so my coworkers are able to edit the names.
You can use following:
var someObject = require('./somefile.json')
JSON can be imported via require just like Node modules. (See Ben Nadel's explanation.)
You would generally want to store it as a global variable, rather than re-loading it on every keyup event. So if the JSON is saved as watchlist.json, you could add
var watchlist = require('./watchlist');
at the top of your code. Then the search command could be written (without needing the for loop) as:
kitten = kitten.toLowerCase();
if (watchlist.entities.indexOf(kitten) !== -1) {
alert('potential watchlist');
}
In the latest versions of Node you can simply use imports!
CommonJs:
const jsonContent = require('path/to/json/file.json')
ES6:
import jsonContent from 'path/to/json/file.json'
You can also import JSon files dinamically, take the following example:
if (condition) {
const jsonContent = require('path/to/json/file.json')
// Use JSon content as you prefer!
}
that way, you only load your JSon file if you really need it and your code will score better performances!
Do you prefer an old school approach?
ES6:
import fs from 'fs' // or const fs = require('fs') in CommonJs
const JSonFile = fs.readFileSync('path/to/json/file.json', 'utf8')
const toObject = JSON.parse(JSonFile)
// read properties from `toObject` constant!
Hope it helps :)
With standard JS objects, one can use destructuring assignment such as:
let obj = {name: 'james', code: '007'}
let {name, code} = obj // creates new variables 'name' and 'code' (with the proper values)
As suggested by some Flux / Redux evangelist, I use immutable.js for my app; can I use destructuring also on immutable List / Map? Of course, one could do:
let obj = immutable.fromJS({name: 'james', code: '007'})
let {name, code} = obj.toJS()
but this seems to have quite inefficient as the objects grow bigger (because the object needs to be deeply jsified first).
With immutable List, the destructuring works quite straightforwardly. This is because destructuring of Arrays works on every iterable (Checking whether something is iterable) and is not subjected just to js Arrays.
With Map, the situation is more complicated. Unlike with List, the destructuring of Map-like structures IS subjected just to plain JS objects and nothing more. Currently, it does not seem that ES community considers this a good idea (see https://esdiscuss.org/topic/extensible-destructuring-proposal)
However, there exist babel-plugin that enables this: https://github.com/vacuumlabs/babel-plugin-extensible-destructuring
Having this plugin installed and enabled in .babelrc, you can simply patch immutable Map to have a .##get method defined:
// main.js, first file loaded
import {Iterable} from 'immutable';
Iterable.prototype[Symbol.for('get')] = function(value) {return this.get(value); };
and everything works (also nested destructuring, or destructuring with default values)
import {fromJS} from 'immutable';
const map = fromJS({author: {name: {first: "John", last: "Doe"}, birthdate: "10-10-2010"}});
const {author: {name: {first, last}, birthdate}} = map;
Disclaimer: I'm one of the authors of the plugin mentioned above.
I am new to programming and F# is my first language.
Here are the relevant parts of my code:
let internal saveJsonToFile<'t> (someObject:'t) (filePath: string) =
use fileStream = new FileStream(filePath, FileMode.OpenOrCreate)
(new DataContractJsonSerializer(typeof<'t>)).WriteObject(fileStream, someObject)
let testList = new List<Fighter>()
saveJsonToFile testList<Fighter> #"G:\User\Fighters.json"
I have previously created an F# record of type Fighter.
When I try to run "saveJsonToFile testList #"G:\User\Fighters.json"", "testList" is underscored in red with the error message: "Unexpected type arguments".
What went wrong? What am I missing?
First, your testList is a value, not a function.
Second, it doesn't have a generic argument, everything in its body is concrete.
If you want it to be a function, give it a parameter.
If you want it to be generic, say so in the body.
let testList () = new List<'a>()
saveJsonToFile testList<Fighter>() #"G:\User\Fighters.json"
And finally, the List you're trying to create probably resolves to F#'s own List, which is a module, not a type.
If you meant to create the .NET's System.Collections.Generic.List, then you need to specify the full type name:
let testList () = new System.Collections.Generic.List<'a>()
But if you meant to create an F# native list, use one of its constructors:
let testList () = []
I have an array in the following format:
agendaItems = [{"topic":"blah", "description:"blah"}, {"topic":"blah2", "description":"blah2"}].
I need to update the values in this array in a handler and handler does not allow global variables to be modified. I know I have to use either CacheService or ScriptProperties. However, I can't seem to make it work:
If I use CacheService, I get something like this: "[object oject][object object]"
CacheService.getPublicCache.put('agenda', agendaItems);
If I use ScriptProperties, I get something like this: ""[Ljava.lang.Object;#429bd3a7"
ScriptProperties.setProperty('agenda', agendaItems');
Am I doing this wrong or is there a better way? Any advice is appreciated.
The Cache class works with strings. You have to use the Utilities.jsonStringify and Utilities.jsonParse methods to convert the array to a string and vice versa. Here is slightly modified code which I use
this.getData = function(id, cacheKey) {
var cache = CacheService.getPrivateCache();
var cachedString = cache.get(cacheKey);
var lstData;
if (cachedString == null) {
lstData = getNotCachedData_(id);
cache.put(cacheKey, Utilities.jsonStringify(lstData));
}
else {
lstData = Utilities.jsonParse(cachedString);
}
return lstData;
}
The ScriptProperties Service also works with strings. It is not obvious due to the not complete documentation, the value parameter has type var and not String, but it is true.
I'm using the JSON lib net.sf.json(http://json-lib.sourceforge.net/apidocs/net/sf/json/package-summary.html) in my scala code.
Also, I'm using the specs BDD framework (http://code.google.com/p/specs/) for unit testing. In the doBefore block, i have the following code:
doBefore {
iter = serversJSON.iterator()
}
serversJSON is a JSONArray object. Outside the doBefore block, I have declared the variables used as follows
var serversJSON:JSONArray = null
var iter:Iterator[JSONArray] = null
But on compilation I'm getting the following error.
error: type mismatch; found :
java.util.Iterator[?0] where type ?0
required:
java.util.Iterator[net.sf.json.JSONArray]
iter = serversJSON.iterator()
I guess the way i have declared the iter object outside doBefore is incorrect. How to fix this?
Please Help
Thank You.
As indicated here, the JSON library's iterator method returns a raw Iterator, not an Iterator[JSONArray]. You'll want to declare it as follows:
var serversJSON:JSONArray = null
var iter:Iterator[_] = null