In the Adobe Javascript DOM most objects classes have a plural class which contains all instances of that object e.g:
Hyperlink (an object for defining a hyperlink object)
Hyperlinks (an object for defining all instances of hyperlink objects)
Other than containing all instances of the singular object, the plural classes has its own properties and methods (so it is not an array).
This seems fairly straight forward to replicate within Javascript, however Adobe is able to use the following syntax:
var singleHyperlink = Hyperlinks[0];
They are able to treat the object as if it were an array. Passing it a numerator inside square braces will return a single objet.
How are they doing this?
Related
I have a variable called jsonAllSignOffs that is created by a .NET JSON service and sent to the client. It is essentially a struct containing various arrays. The values of these arrays are arranged such that if you took the nth element of each array, all together that would be the collected properties of the nth Sign Off. Obviously a list of Sign Off objects containing these properties would be better, but unfortunately this is a legacy application and changing it's architecture in this manner is out of scope.
What I'm trying to do is create a variable called jsonUserSignOffs that is essentially a subset of jsonAllSignOffs with all the same properties. However jsonAllSignOffs is not a type that I can instantiate. I figured declaring a variable and assuming the properties by assigning into them would "build" the object, but apparently that's not the case.
var jsonUserSignOffs;
jsonUserSignOffs.isAuthor = jsonAllSignOffs.isAuthor; //error jsonUserSignOffs is undefined
Since javascript doesn't support classes and is pretty lax with variables I figured the only way to create a struct like jsonAllSignOffs was to declare the variable and assign values to it's properties. I know these properties are not defined anywhere, but I thought assigning values to them would instantiate them at the same time. I come from a C# background where I would use a class. Javascript is less familiar to me, and I'm unclear on how to proceed.
Try this
var jsonUserSignOffs = {}; //creates an empty object using object literal notation
jsonUserSignOffs.isAuthor = jsonAllSignOffs.isAuthor;
OR:
var jsonUserSignOffs = {
isAuthor: jsonAllSignOffs.isAuthor
};
I have written a NPAPI Plugin using firebreath framework. I am able to pass simple numeric values from Javascript and access them in my (C++)plugin, perform operations and then return the result. I would like to know how to operate on vectors now i.e arrays. I do not want to allocate new array inside my plugin and copy the array from JavaScript(Although I have no clue on how to do it). How can I directly access the JavaScript array in my plugin ? Is there a special way to do it ?
From the Firebreath website:
Javascript objects can be used with the FB::JSObjectPtr type. Examples of JavaScript objects that you may want to use include:
Javascript objects (with methods and/or value members)
Javascript Arrays (that you plan to modify; otherwise you can use a container type)
Javascript methods for callback
Arrays are objects; get values with getProperty(n) or getProperty("length") etc
You can also use methods like ->invoke("push", FB::variant_list_of(val)) etc
(on the JSAPI method in order to use the JSObjectPtr type you should use a method that looks something like:)
void doSomethingWithAnArray(const FB::JSObjectPtr& array) { ... }
Also remember that FireBreath can't tell what type of js object it is, just that there is a js object; you'll have to do your own error detection for the case where they don't give you an actual array but some other object instead.
It's something I'm missing in Chrome's console. I'm following a screencast where a javascript object is displayed in a browsable tre-like structure like this:
(arrow here) Object
When typing this for example, I'm only getting an empty array:
jQuery()
If you want to browse the elements within the jQuery object, you'll have to select something first.
$() returns an empty jQuery object.
If, on the other hand, you want to browse the methods/properties of the object created, you should use the console.dir method, which will give you a more in-depth view of your object:
console.dir(jQuery());
Here's the fiddle: http://jsfiddle.net/asYay/
The Object you are getting by typing
jQuery()
is an empty jQuery Object, which is represented by the same syntax as an empty array
[]
jQuery Objects are Array-like objects of html elements you select from the body. In your case, the selector (the first argument of the function which you would normally write into the function) is empty, so there is nothing to search for for jQuery and it returns an empty object.
If you would pass an argument with a DOM element (for example the body), it would return the body within the array
jQuery('body') //=> Array with the body html element inside
Note that HTML elements within the jQuery Object are normally not represented the same as standard objects in the Google Console. For standard objects, you will get the Object with its properties as a tree structure with an arrow before it to expand it (like the one you see in the screencast), with HTML elements, you get the DOM node, but no properties or methods to expand.
To see the difference, try this:
Display of an instance or a standard object in the Chrome console:
var object = {
hi: 'im an object',
and_i: 'am represented as a tree like structure',
i_can_haz: function() { return 'this is great, it shows all the stuff' }
};
If you type again:
object
You will get the Chrome Console Object representation.
For a HTML Object, just do this
var object = document.getElementsByTagName('body');
And if you want to access its properties and functions, use the dir method
dir(object);
You can use dir on virtually any object to access the properties
And object will be a representation of the body element. As said before, all jQuery does when selecting is putting these elements into an array, essentially.
In C++ I have learned that Variables are the used for Data Storage and Objects are the instance of a Class. But in JavaScript I have seen people referring variables as an Object. Why are the Variables referred as Objects in JavaScript. I am getting confused with that.
In javascript, a variable can either hold a single piece of data itself (like the number 3 or a text string like "Having Fun") or it can hold a reference to an entity like an array or an object.
An object in javascript is essentially a container in javascript. It can hold multiple properties that are each accessed with a key. For example, an object could have a property named "name" that contains the value "Bob", it could also contain a property named "Age" with a value of 29. Objects can have as many properties as one wants. As such objects are essentially a collection of multiple variables, where each variable has its own name and value.
There are technically no "classes" in javascript so it doesn't work the same way that C++ does. Javascript uses prototypes and objects instead of classes and instances in C++.
In reference to the specifics of your question, a variable can contain a value or a reference to an object. There is no right or wrong, it depends upon the problem to be solved.
Some examples:
var person = {}; // a variable `person` that contains a reference to an empty object
person.name = "Bob"; // add a property "name" and value to the object
person.age = 29; // add a property "age" and value to the object
var numPeople = 12; // a variable that just contains a number
Javascript is prototype based OOP language.
Prototype-based programming is a style of object-oriented programming
in which classes are not present, and behavior reuse (known as
inheritance in class-based languages) is performed via a process of
cloning existing objects that serve as prototypes.
In Javascript we use DOcument Object Model(DOM) for refering to different elements of web page.
so when we refer to window,document,form they all are objects and variables are declared like
var x=0;
following will be example of object assignment.
var w=window;
Does anyone know of decent examples of custom JavaScriptConverter classes? The MSDN's only example is of one converting a ListItemCollection. What about custom classes? What if the custom class has a property of another custom class? Do we need two converters? Any references would be greatly appreciated.
Thanks!
You should only need one converter. The example basically outlines how to use the JavaScript converter for any custom class. It doesn't need to be a class that is part of the framework.
It will also work for any properties of a custom class that are themselves a custom class.
JSON views objects as collections of key/value pairs, so the documentation example shows how you should take any properties of your object and put them into Dictionaries (a type of Key/Value pair object). If you need a nested custom type, you can just nest Key/Value pairs inside of your main Key/Value pair collection.
Also, unless you have very specific needs (built-in serialization either won't work, or doesn't output what you want), you should just use the JavaScriptSerializer class.
JavaScriptSerializer serializer = new JavaScriptSerializer();
MyCustomObject obj = new MyCustomObject();
string json = serializer.Serialize(obj);
MyCustomObject object2 = serializer.Deserialize<MyCustomObject>(json);
That should do what you want in 95% of cases.