locutusjs / locutus

Bringing stdlibs of other programming languages to JavaScript for educational purposes

Home Page:https://locutus.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ES6 Support?

KnightYoshi opened this issue · comments

Will this project be getting ES6 support?

Using class_exists on an ES6 class does not return true, because ES6 classes do not have a prototype.
I think the only way to tell the difference between a class and a regular function would be to check if it is a function, but doesn't have the arguments property that normal functions have.

Never mind, "... arguments may not be accessed on strict mode functions ..." Any ideas?

arguments.callee is no longer supported, but arguments is (though I think I have read it is discouraged in favor of rest parameters).

'use strict';

function myfunc () {
  console.log(arguments)
}

myfunc.arguments

will throw an error, because the arguments property may not be accessed outside of the function.
"TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them"

I don't understand what you're doing here. myfunc.arguments is never supposed to be populated outside of the function including in non-strict mode.

A more relevant demo would be:

'use strict';
function myfunc () {
  console.log(arguments)
  console.log(arguments.callee)
}
myfunc();

In this example, the first logging works, but not the second.

From MDN, "arguments as a property of Function can no longer be used."

That doesn't seem technically correct now per FF 44, since the following logs a value (usage within a function):

function myfunc () {
  console.log(myfunc.arguments)
}
myfunc();

When used outside, it returns null rather than undefined:

function myfunc () {
}
console.log(myfunc.arguments);

Anyways, it looks like that behavior is out the door anyways, but arguments alone is in wide use, so I highly doubt it is more than deprecated.

The arguments property inside the function works fine. That's not the issue, I don't want to use it, that would be pointless. I wasn't using using it outside of the method. I was thinking it would be useful to see if the function has has the property to determine if was a class or not

In the class_exists function to determine if it's an ES6 class to check if the property exists.

if(typeof cls === 'function' && !cls.arguments) {
return true; // is a class
}

Though ES6 classes are not on the window object. So I suppose retrieving it would be the first thing. Maybe ... cls = eval(cls) since window[cls] can't be used?

Ah, sorry, hadn't looked carefully.

I think http://stackoverflow.com/questions/29093396/how-do-you-check-the-difference-between-an-ecmascript-6-class-and-function has some solutions which should work independent of strict mode and without eval.

That's actually good, that helps. Would the class in question need to be passed to see if it's undefined then instead of a string of it's name? I have been looking around, but haven't found a way to get the equivalent of window[funcName] for ES6 classes, since unless it's a class expression it's in the lexical scope (something like that I believe).

class Me {
  constructor () {
    return true;
  }
}
console.log(window['Me']); // undefined

var My = class My {
  constructor () {
    return true;
  }
}
console.log(window['My']) // function My()

So from what I can tell unless Me is passed without being a string there isn't a way to then check if it's a class or not. What is your thought, since PHP's class_exists calls for a string?

If there is no way to introspect, then I think we can either accept functions only and make the function experimental, or accept a string and if a function is passed, use that (though the latter of course suffers in case the user meant to ask whether a string was a class).

It'd also be kind of cumbersome because all of this would assume the user had declared the variables being checked or otherwise, they would be "not defined" errors.

The only other option I can think of to keep the string argument only would be to check the innerHTML of script tags (or reload external ones by Ajax) to parse for class constructions but that would be way too cumbersome.

Hi there, just a quick note that Locutus now supports ES6. More information here http://locutus.io/2016/05/announcing-locutus/. An example function can be seen here http://locutus.io/php/network/ip2long/ which is ES6 in source, but transpiled to ES5 before publishing to npm.

As for your question wether we can detect ES6 classes via the class_exists PHP port, I haven't done the research myself yet. If we can do it without hacks that would be cool, but the innerHTML trick does not appeal to me either.

Closing for now but welcoming more input on this.