JSON.stringify() is this object and function standard to javascript? - javascript

I have never seen it before until today and it seems to just work without including any .js or framework. is this a standard object in javascript? if so where can I find the documentation for it and other uncommon Objects that are available in javascript

Looks like you need to learn about MDN
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

It's part of the ES5 specification. See http://es5.github.io/ for the whole annotated spec and for this function specifically see http://es5.github.io/#x15.12.3
See also http://caniuse.com/#search=json for a browser support matrix.

Yes it is. It's a standard object, in common use and works in all current browser versions. Documentation here.
The only time you'll have a problem with it is in old IE versions; if you need to support them, you'll need to use a polyfill library like this one.

Related

How do I get tanh in jsweet?

I am using JSweet to transpile Java into Javascript and I need to use Math.tanh() but it's not available in the jsweet.lang.Math object, but I see it in the source:
https://github.com/cincheo/jsweet/blob/426e379958fc5392f8328d8e431caac0cf95653e/core-lib/es6/src/main/java/def/js/Math.java#L161
It's also missing from the API documentation:
http://public.jsweet.org/apidocs/releases/org/jsweet/candies/jsweet-core/1.2.0-20161222/jsweet/lang/Math.html
Which Math is it using and how do I use the one that implements tanh() ?
If you want to use Math.tanh(), which is an ECMAScript 6 feature, you should target ES6 in your pom.xml and add a reference to core-lib 6.
Anyway, if you feel that way, you can also use this MDN equivalent using just simple Math.exp() calls: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tanh#Polyfill
Easy to use in JSweet :)
Hope this helped.

How to detect if browser is compatible with ES2015 [duplicate]

This question already has answers here:
Javascript ES6 cross-browser detection
(10 answers)
Closed 7 years ago.
I have a big chunk of JS libraries that I should rewrite since it's really old and outdated. So, I decided to come up with a solution where I would just use most of ES2015 features like rest parameters.
The thing is, I am sure all the clients would not have their browser up-to-date and I am confused whether I will face any issue regarding their browser is compatible with my new JS libs.
So, I was wondering if I could detect whether the client browsers are compatible with ES2015. And if not, I would just include my old JS library.
I am looking for a solution like Conditional comments, but I am getting nowhere to solution.
Any help in HTML, JS or PHP is appreciated. Please kindly suggest your advice.
I was wondering if I could detect whether the client browsers are
compatible with ES2015. And if not, I would just include my old JS
library.
You cannot do that, simply because AFAIK there's no browser that fully supports ES2015. Also, you don't really want to maintain two different versions of your code, because it's painful and it could get messy really quick.
The approach nowadays is to use a transpiler, which is sort of a compiler that compiles your ES2015 code to ES5 (the JavaScript that every browser knows). It is still kind of messy, but at least you get to write only one version of your code, and it's ES2015.
I think Babel (formerly 6to5) is the most popular transpiler. You can check out their website for getting started.
As to answer your actual question,
How to detect if browser is compatible with ES2015
You can do that in many ways. I'm not sure what could be the most reliable one, but for example you could simply try on your browser's console:
'Promise' in window
If this statement returns true, then the current browser supports promises, which is a new feature of ES2015, so you could assume that it supports most of the features of ES2015.
This is not enough for most cases though; you may want to be 100% sure that what you're using is not going to throw a SyntaxError in any old browser.
A solution could be to manually check for each feature you want to use. For example, if you need the Fetch API, you could create a function that tells you if the current browser supports it:
function isFetchAPISupported() {
return 'fetch' in window;
}
You can do this for any new API you need. However, if you need something syntactically different, I think your only bet is eval() (as Katana314 suggested). For example, in order to check support for rest parameters you could do:
function isRestSupported() {
try {
eval('function foo(bar, ...rest) { return 1; };');
} catch (error) {
return false;
}
return true;
}
This works great in Firefox, because the rest parameters are supported.
It works as well on Safari, returning false because rest parameters are not supported there yet.

Writing ECMAScript5 compliant code

I want to build a library in JavaScript/JScript/ECMAScript...whatever you want to call it, which will target modern standards (HTML5, CSS3, ESv5) with that in mind, any browser that supports the standard! Now I know there are already plenty of useful libraries out there, i.e. jQuery and MooTools. Of course they're great and I already use those where necessary, but I should not be forced to jump on the same bandwagon as every other developer just because it's popular!
So for the sake of the following questions, let us not concern ourselves with 3rd party libraries such as jQuery and MooTools. Lets get down to nitty-gritty JavaScript/JScript/ECMAScript.
Firstly, I asked a question prior to this (What's the difference between JavaScript, JScript & ECMAScript?), as I did not know what the difference was.
Thankfully I concluded the following:
ECMAScript is the language specification. JavaScript and JScript are dialects of ECMAScript.
JavaScript is Mozilla's implementation of ECMAScript.
JScript is Microsoft's implementation of ECMAScript.
OK, that was a nice simple answer wasn't it? But here's some questions which stem from that:
is "JavaScript" supported in non-mozilla browsers, and to what extent?
is "JScript" supported in non-microsoft browsers, and to what extent?
Based on those two questions, I did a little digging and ran a simple test on IE9, FF14 and GC19.
Here is the test code:
<!DOCTYPE html>
<html>
<head>
<title>HTML 5 Template</title>
<script language="JavaScript1.3">
jsver = "1.3";
</script>
<script language="JavaScript1.4">
jsver = "1.4";
</script>
<script language="JavaScript1.5">
jsver = "1.5";
</script>
<script language="JavaScript1.6">
jsver = "1.6";
</script>
<script language="JavaScript1.7">
jsver = "1.7";
</script>
<script language="JavaScript1.8">
jsver = "1.8";
</script>
<script type="text/javascript">
document.write("<B>Your browser supports JavaScript version " + jsver + ".</B>")
</script>
</head>
<body>
</body>
</html>
The results were: IE9 = JSv1.3, FF14 = JSv1.8, GC19 = JSv1.7
OK, then I ran this test, which tests for ECMAScript version 5 support:
http://kangax.github.com/es5-compat-table/#showold
Again using the same browsers (IE9, FF14, GC19), ESv5 seems to be fairly well supported!
Now comes the tricky bit! I come from a Microsoft background, and write software using languages like C#, ASP.NET etc, so naturally, my IDE of choice is Visual Studio (currently 2010). When I look through the JavaScript intellisense I see things like ActiveXObject, Array, Object, etc.
Should I trust VS2010's intellisense?
Where can I find a reference of ESv5 supported objects and features?
How do I detect if a browser supports a particular object or feature?
Is there anything better than VS2010 out there that will help me write compliant ESv5 code?
Is it safe to override implementations of existing objects like Object, Number, Boolean etc, or should I just extend the existing implementation?
Finally, concerning myself with jQuery. Let's say I can't be bothered to write the core compliancy & functionality myself, can I just write my library to sit on top of jQuery...or is this just a copout?
1) Nope. Certainly it won’t restrict to just valid ECMAScript.
2) http://kangax.github.com/es5-compat-table/ is always useful.
3) You can just check to see if it’s defined. E.g.
typeof(Array.isArray) === 'function'; // true in newer browsers, false in IE8
4) Your best bet is to read the spec! Using "use strict"; in your code will also catch some classes of errors and it good practise. More explanation on strict mode at http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
5) Certainly I wouldn’t replace the original objects. If you’re going to extend properties I’d first double-check that a compliant implementation doesn’t already exist. E.g. PrototypeJS added (before browsers implemented it) a document.getElementsByClassName. When browsers started implementing it natively they found out that sites using Prototype were still using the slow JS-based version. The fix was just to wrap Prototype’s definition in a if (document.getElementsByClassName == undefined) { }
2) I find the Overview you provided pretty good. What else do you want?
3) A good library to even out the differences between the browser is ES5-shim. It autodetects the features and provides shims for the browsers who lack support.
4) Always use "use strict"; and a have good editor of your choice which has some kind of code-highlighting and perhaps code-completion. I use the browser consoles or firefox scratchpad (silly name for a good tool) for quick hacking sessions and put it all together in notepad++ (= the IDE of your choice).
5) Augmenting the native javascript objects has been debated a lot and has pros and cons. Prototype and MooTools go this way. jQuery takes the other way and provides a separate object for that. I personally prefer to leave the native (and especially host) objects alone and have your own namespace with the functions you need.

Javascript regex shorthand?

I'm trying to enjoy some of the awesome javascript code golf submissions on anarchy code golf, but I keep seeing things like:
for(;s=readline();)print("h"+/t.*/(s))
...which was the JS winner for: http://golf.shinh.org/p.rb?ttp
I don't understand how that is correct javascript syntax, and I even tried resubmitting that, but it said object is not a function, which is something along the lines of what I would expect to happen.
Was this some kind of glitch or shorthand or something in an older javascript version?
Was this some kind of glitch or shorthand or something in an older javascript version?
More or less, yes. According to that site's version info, it uses SpiderMonkey (Mozilla's JavaScript engine), which used to have the feature that regular-expression objects were callable; that is, that if re was a regular-expression object, then re(...) was equivalent to re.exec(...). That feature was removed in this change, a result of Bug 582717, and that site has since updated to a version that incorporates that removal.

html specification

I cannot seem to find a link for a HTML api.
I want to see element.innerHtml, element.OuterHtML. Basically all methods I can invoke in a javascript function to get an elements rendered/not rendered text. Thank you very much
I think you are looking for the DOM API.
You can find it on the w3 site.
The HTML specification is about the structure of the HTML document, not how to access it with innerHTML and other methods.
Try out : https://developer.mozilla.org/en/DOM/element
MDC Doc center is a great reference to use for everything relative to HTML. You can generally find the pages from MDC directly from google. Here I searched "element MDC"
Cheers,
-stan
You want the spec for the Document Object Model.
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html
http://www.w3.org/DOM/DOMTR
A cleaner list can be found here:
https://developer.mozilla.org/en/DOM/element
There is the W3C DOM Level 2 HTML specification which documents some of the bindings you may be interested in. (Seven years ago I put this into a more browsable format here: http://objjob.phrogz.net/html/hierarchy)
However, some of the properties you discuss, such as innerHTML are considered "DOM Level 0". They were implemented by browsers before there was a standard.
You might also be interested in the MSDN DHTML reference, which documents properties, methods, and more supported by various versions of IE. While some of them are non-standard, the documentation these days generally indicates which items are standard and which are proprietary extensions.
Finally, there is the Gecko DOM Reference which provides good information from Mozilla.

Categories

Resources