Converting objects type in javascript? - javascript

I am using the new(ish) chrome.storage.sync API for a chrome extension and the when saving objects, it completely ignores functions.
This means that when I retrieve objects from storage, I have the data in the object, but no functions.
Is there a way to reconnect the object's data to its functions?
Note: I'm not doing anything weird in the functions (like adding variables to it or changing its closure)

You basically need to run your object through a constructor function after it comes out the data storage thing.

I don't think there is a way to save functions persistently.
You could save an object with like.. self.fns=['fn1','fn2']; and on reload, add the functions back based on that.
But you should probably change your design if you are dynamically adding functions to things and need to save them that way.

Related

Persistant variable with Gmail scripting (Google Apps Script) [duplicate]

In a Google spreadsheet using the Script Editor, I do function calls, but I am not quite sure if the best way to store persistant data (data that I will continue to use) is to use global variables (using objects, arrays, strings), or there is a better way to store data.
I don't want to use cells which could be another way.
Another question, is it possible to create (pseudo) classes in this environment? Best way?
Both ScriptProperties and ScriptDB are deprecated.
Instead, you should be using the new class PropertiesService which is split into three sections of narrowing scope:
Document - Gets a property store that all users can access within the current document, if the script is published as an add-on.
Script - Gets a property store that all users can access, but only within this script.
User - Gets a property store that only the current user can access, and only within this script.
Here's an example persisting a user property across calls:
var properties = PropertiesService.getScriptProperties();
function saveValue(lastDate) {
properties.setProperty('lastCalled', lastDate);
}
function getValue() {
return properties.getProperty('lastCalled');
}
The script execution environment is stateless, so you cannot access local variables from previous runs, but you can store getScriptProperties() in a local variable because it will be re-run for each return trip to the server so it can be called in either method.
If you need to store something on a more temporary basis, you can use the CacheService API
Persistent data can be stored using the Class ScriptProperties:
http://code.google.com/googleapps/appsscript/class_scriptproperties.html
All values are stored as a string and will have to be converted back with the likes or parsInt or parseFloat when they are retrieved.
JSON objects can also be stored in this manner.
My experience has been that every query to retrieve or store values takes a long time. At the very least, I would cache the information in your javascript code as much as possible when it is safe. My scripts always execute all at once, so I don't need to keep global variables as I simply pass the retrieved data arrays around, manipulate them, and finally store them back in one fell swoop. If I needed persistence across script invocations and I didn't care about dropping intermediate values on close of the webpage, then I'd use globals. Clearly you have to think about what happens if your script is stopped in the middle and you haven't yet stored the values back to Google.

What is the performance cost of DocumentSnapshot.data() method in Firestore web api?

In a web application using firebase's firestore, one may keep the DocumentSnapshot object in a variable and call data method to retrieve data contained in the document whenever required, or call data method once and store it in a variable then use it.
Is data method simply an accessor function and does not have a dramatic impact on performance or should repetitive calls of the method be avoided?
In a specific use case, is there a good reason for keeping result of data method's result, its id and ref properties in different fields in a react component's state rather than keeping DocumentSnapshot instance itself?
There is nothing terribly expensive about a call to data(). You can examine the source code yourself.

Post representation in JavaScript?

Question 1: If there a post such as this one. How would I represent it in JavaScript? I thought was JSO and parse. Other programming language like C++ and JAVA a main function and have map references but where does JavaScript store it's objects?
Question 2: Oh and do I add Event Handlers to each of the HTML representatives elements?
The basic data structure in JavaScript is the object, you would probably use one (or, more likely, a structured collection of them along with more specific data structures such as arrays) to describe the data structure that makes up a webpage.
The main function in Java and C++ is the entry point to the program. In JavaScript, the entry point is the top of the program.
JavaScript stores its objects in memory and references to them in variables and as properties of objects.
You would only add an event handler to an HTML Element object if you wanted to listen for an event on it.
People are saying store objects in variables but it's I was also told its better to hash them and create references to them in an array.
You use the data type most appropriate for the data you are working with.
If you have a:
simple piece of data, use a variable.
collection of ordered data, use an array (or maybe a Set if you are using ES6)
collection of unordered data, use an object (or maybe a Map if you are using ES6)
… and nest to whatever level makes sense.
You might also create a custom data type with a constructor function or (if you are using ES6) a Class.

extracting all used javascript variables

I have a big object defined in the global scope called global. I would like to dynamically find all the referenced properties under my variable global. That is, all the properties that were accessed during the execution of the code.
I want to do static code analysis to extract all the referenced properties under my variable. I can search for these patterns: global.PROPERTY_NAME AND global[PROPERTY_NAM]. However, what about the complicated cases like these ones
var tmp="PROPERTY_NAME";
global[tmp]
OR
var tmp=global;
tmp.PROPERTY_NAME
and the other ones?
I don't want to get all the variable's properties. I only want a list of the referenced ONES!! the properties that were referenced in my source code only
After your edit:
What you're looking for is JavaScript Proxy objects. Here is a tutorial on how to do this using them.
Proxy objects let you wrap an object and execute a method whenever its properties are accessed. Unfortunately as it currently stands they are not widely supported.
This is currently only way in JavaScript to accomplish this without changing your original global object.
You can turn them on in Chrome by enabling experimental JavaScript in the about:flags tab.
Before your edit:
The feature you're looking for is called reflection, JavaScript supports it well and natively
Here is some code that iterates through an object and gets its properties
for(var prop in global){
if(global.hasOwnProperty(prop)){ //this is to only get its properties and not its prototype's
alert(prop+" => "+global[prop]);
}
}
This is fairly cross-browser. More modern browsers allow you to do this in simpler ways like Object.keys(global) which returns an array containing all its enumerable properties, or Object.getOwnPropertyNames(global) which returns both enumerable and not-enumerable properties.
Due to the dynamic nature of JavaScript you won't achieve that with static code analysis. Think about cases like this:
var prop = document.getElementById('prop').value;
global[prop];
Impossible. The alternative, dynamic analysis, would mean that you modify your global object to log access to its properties, then run the code. This is easily possible in JavaScript but it won't help you either because how would you assure that you have covered every possible access? Especially in a 5 MB JavaScript, there are most likely edge cases that you will oversee.
So, if you can't narrow down your requirement, it won't be possible.

reconstruct object functions retrieved from localStorage

I'm using JSON.stringify and JSON.parse to store and retrieve objects from localStorage. However, it appears that JSON.stringify strips out the instance functions from the object. Thus, after JSON.parse, I can no longer call myObject.doSomething(). I know that I can attach this function manually: myObject.doSomething = MyClass.prototype.myFunction, but that'll be troublesome if this action is repeated many times in the web app. How do people normally do this in JavaScript?
JSON obviously does not hold onto the functions themselves is only stores simple typed variables. The way I have addressed this in the pass is to be a restore method in my class and simply call that method with the data from JSON so as to re-populate the class with the data that belongs in it.
I have done this extensively with the Value Object ( VO ) design pattern in my code base and it has worked quite well for me. Just a word of a caution though, Ie7/Ie8 are not terribly friendly with this approach if you try to communicate across windows. As I recall I think it is IE7 that does not return the right "typeof" for some properties so I ran into a whole bunch of challenges in my restore when cross-window communication was involved.

Categories