14

I stumbled upon a function called v8Locale in Chrome's Developer Console. I was curious so I entered the function to get the source code, and it revealed the following code:

function (a){
native function NativeJSLocale();
var b=NativeJSLocale(a);
this.locale=b.locale;
this.language=b.language;
this.script=b.script;
this.region=b.region;
}

I started searching on the Internet and found this file which seems to be the source (it looks like it has been minified though).

I have no idea what the native keyword means here. When I try to make something like this myself:

function bar() {}

function foo() {
    native function bar();
}

I get the following error message (as I expected, actually):

SyntaxError: Unexpected token native

How is it possible that the v8Locale function contains the native token, and what does it mean/do?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    Source path `src/extensions/experimental/i18n.js`. Sounds like a chrome extension file. You can write chrome extensions in a superset of JavaScript. – Raynos Jul 18 '11 at 12:42
  • Maybe it's the code of the browser. For example if you see `window.history.back` in Chromes console, and you see `function () { [native code] }` – Saeed Neamati Jul 18 '11 at 12:44

4 Answers4

10

That is used to tell v8 that the function is implemented in C++ code

Matt
  • 43,482
  • 6
  • 101
  • 102
2

The native keyword is not defined in the ECMAScript 5 specification.

Sounds like it's part of a chrome extension

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • 1
    It's not defined, but it is reserved... so there are most certainly some people who have an idea of what it's for! – Alexis Wilke Jun 14 '14 at 06:00
  • 1
    @AlexisWilke It was reserved because it's a keyword in Java. Same with public, private, volatile, synchronized, transient, etc. This isn't quite hard and fast, though. I think the reserved a few others just in case. – David Ehrmann Jun 14 '14 at 06:20
  • @DavidEhrmann Why would keywords in Java be relevant to the Javascript language? You say "just in case" ... just in case what happens? – Luke Griffiths Mar 05 '20 at 22:23
  • @LukeGriffiths I realize the languages are actually very different, but the syntax is very similar, the "Java" bit of the name was at least intentional, and it makes sense to reserve keywords that similar, contemporary languages use. Java reserved `goto` and `const` because of C and C++. Here's the Javascript list: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Future_reserved_keywords . It's pretty obvious what "just in case" is if you look at the list (especially the ES v1-v3 list). – David Ehrmann Mar 05 '20 at 22:55
1

According to the latest MDN Web Docs, the native word is no longer reserved in javascript. Is also usable in Node JS:

alec@MacBook-Air ~/process/tmp/shed/cli (main) $ node -v
v16.9.1
alec@MacBook-Air ~/process/tmp/shed/cli (main) $ node
...
> let native = 'XLM'
undefined
> console.log(native)
XLM
undefined
> 
Did Alik
  • 199
  • 2
  • 7
0

ActionScript, which is also based on ECMAScript, defines the native keyword here:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#native

They offer an example with code:

native function functionName();
class className { 
  native function methodName();
}

And there is the description:

Specifies that a function or method is implemented by Flash Player in native code. Flash Player uses the native keyword internally to declare functions and methods in the ActionScript application programming interface (API). This keyword cannot be used in your own code.

As implied by Matt, functions marked as native are implemented in the interpreter so you cannot yourself define a native function (unless you tweak the source code of your JavaScript interpreter...)

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156