Does JavaScript support a ByteArray class in the browser? [duplicate] - javascript

This question already has answers here:
How to store a byte array in Javascript
(4 answers)
Closed 7 years ago.
The ByteArray class provides methods and properties to optimize reading, writing, and working with binary data.
How to use byte arrays tutorial.
I'm looking for a very similar API as the one linked.
I'm looking for a class the browser provides not hack or workaround. The linked question does not provide the answer. If it does please provide a link to the documentation.
Some one linked to another question but that did not answer my question.
Update: Someone off list pointed me to this class:
https://gist.github.com/sunetos/275610#file-bytearray-js
It has most or all of the read methods but none of the write methods and it's not native to the browser.

Modern browsers support Uint8Array, one of JavaScript's TypedArray classes.
var data = new Uint8Array(8);
var data = new Uint8Array([0x10, 0x12]);
It does not have built-in methods for encoding/decoding Unicode strings. See Converting between strings and ArrayBuffers for examples of how to do that.

The answer is yes, here are the relevant docs since you just wanted this...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
The linked docs for each type in the above docs show the methods available on each type. IE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array

Related

For JavaScript, how do properties distinguish from methods? [duplicate]

This question already has answers here:
Example of Properties vs. Methods in JS
(3 answers)
Closed 3 years ago.
In chapter 4 of Eloquent JavaScript, it reads: "[Data types, namely, strings] have built in properties. Every string value has a number of methods. Some very useful ones are slice and index which resemble the array methods of the same name.
For me, the excerpt that I've cited from Eloquent JavaScript appears to use the terms "property" and "method" interchangeably.
From MDN Web Docs, I understand that "A JavaScript property is a characteristic of an object, often describing attributes associated with a data structure.
Additionally, MDN's glossary defines a method as a function which is a property of an object.
Can anyone assist in distinguishing the terms "property" and "method" (as used in JavaScript) from one another?
Short answer
Property is something "about" the string e.g. its length.
Method is something that can be done to the string e.g. slice

Define an array of mixed types in C# [duplicate]

This question already has answers here:
How to add different types of objects in a single array in C#?
(11 answers)
Closed 6 years ago.
I'm putting together a little C# project - I am rewriting something I had written in JavaScript as C#.
Part of my project requires an array of arrays with mixed content in. In JavaScript I just initiate the array:
Log: []
then use push:
Log.push(logEntry);
Where logEntry is:
string, string, string, int, int, int
The ints may also be empty.
The purpose of this is to store data before JSON encoding it and sending it to a web API on another server.
Can someone point me in the right direction to do this in C#, as all the answers I find seem to want to only store the same type of data.
Thanks in advance,
Tony.
Here is how you can do it
Use List<object> (as everything is derived from object in C#):
var list = new List<object>();
list.Add(123);
list.Add("Hello World");
Also dynamic might work for you (and your javascript background)
var list = new List<dynamic>();
list.Add(123);
list.Add(new
{
Name = "Lorem Ipsum"
});
If you wan't to use dynamic you really need to know what you're doing. Please read this MSDN article before you start.
But do you need it?
C# is a strongly-typed and very solid programming language. It is very flexible and great for building apps using object-oriented and functional paradigms. What you want to do may be fine for javascript, but looks a bit ugly with C#. My recommendation is: use object oriented programming and try to build models for your problem. Never mix types together like you tried. One list is for a single data-type.
You can try using the ArrayList class.
Another way for a heterogeneous collection of objects is to use List<Object>
The ArrayList class is designed to hold collections of objects. The objects can be of different Type. However, it does not always offer the best performance.
From MSDN
The ArrayList class is designed to hold heterogeneous collections of
objects. However, it does not always offer the best performance.
Instead, we recommend the following:
For a heterogeneous collection of objects, use the List (in
C#) or List(Of Object) (in Visual Basic) type.
For a homogeneous collection of objects, use the List class.

Is there a more compact way to serialize a JavaScript object with duplicate object values? [duplicate]

Is there a standard way of referencing objects by identity in JSON? For example, so that graphs and other data structures with lots of (possibly circular) references can be sanely serialized/loaded?
Edit: I know that it's easy to do one-off solutions (“make a list of all the nodes in the graph, then …”). I'm wondering if there is a standard, generic, solution to this problem.
I was searching on this same feature recently. There does not seem to be a standard or ubiquitous implementation for referencing in JSON. I found a couple of resources that I can share:
The Future for JSON Referencing
http://groups.google.com/group/json-schema/browse_thread/thread/95fb4006f1f92a40 - This is just a discussion on id-based referencing.
JSON Referencing in Dojo
http://www.sitepen.com/blog/2008/06/17/json-referencing-in-dojo/ - An implementation in Dojox (extensions for the Dojo framework) - discusses id-based and path based referencing.
JSONPath - XPath for JSON
http://goessner.net/articles/JsonPath/ - This seems to be an attempt at establishing a standard for path based JSON referencing - maybe a small subset of XPath (?). There seems to be an implementation here but I kept getting errors on the download section - you might have better luck. But again this is no where close to a standard yet.
There is the "JSON Reference" specification, but it seems it didn't got over the state of an expired Internet draft.
Still, it seems to be used in JSON Schema and Swagger (now OpenAPI) (for reusing parts of an API description in other places of the same or another API description).
A reference to an object in the same file looks like this: { "$ref": "#/definitions/Problem" }.
Douglas Crockford has a solution that uses JSONPath (an Xpath-like syntax for describing json paths). It seems fairly sane:
https://github.com/douglascrockford/JSON-js/blob/master/cycle.js
There is no canonical way to achieve that. JSON does not have a native support for references, so you have to invent your own scheme for unique identifiers which will act as pointers. If you really want to make it generic you could use the object identifiers provided by your programming language (eg. object_id in Ruby or id(obj) in Python).

Get exact syntax of javascript base functions [duplicate]

This question already has answers here:
Where can I find javascript native functions source code? [duplicate]
(3 answers)
Javascript native sort method code
(3 answers)
Closed 8 years ago.
Is there a way to know how test (for regex) or cos (for math) or substring (for string) or splice (for array) have been coded in the exact syntax either by a way in Javascript (like alert(function)) or by seeking in the files that come with Firefox?
Well, the source code of javascript likely isn't written in javascript itself, so you would be unable to just print it to the browser console or alert.
You can browse the source code of google's V8 engine, and see if it helps you ;)
http://code.google.com/p/v8/
These functions are actually implemented differently by browsers in one of the OS native languages.
So these algorithms are written in c, c++, c#, java etc. but not in JavaScript.

Howto convert E4X XML Elements into JSON Notation [duplicate]

This question already has answers here:
Convert XML to JSON (and back) using Javascript
(14 answers)
Closed 8 years ago.
I have a server Implementation of ECMA Script including the ability to use E4X. Because this is pretty elegant for people don't know JavaScript and JSON Notation and we want to make an API which is most easy to learn i want to use this for my API.
I'm currently evaluating if i can use this in my environment. One Showstopping feature that i must use is to convert those XML Objects of E4X into JSON compatible JavaScript Objects or Strings on the fly. I can't use XSLT here because i have to stay inside JavaScript.
So the question is, is there an easy way to convert E4X XML Elements into JSON? Or do i have to write some code to convert it myself?
You can use XSLT to convert your XML to JSON.
For instance using: http://code.google.com/p/xml2json-xslt/
However you can end up with a very XMLish and unnecessarily complex JSON. That will make your code more difficult to write and maintain.
An API is generally meant to be stable in time, so may be some dedicated XSLT for each calls may be a better option than a generic one.

Categories

Resources