Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 'native' keyword mean in JavaScript?

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?

like image 253
pimvdb Avatar asked Jul 18 '11 12:07

pimvdb


2 Answers

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

like image 109
Matt Avatar answered Sep 22 '22 21:09

Matt


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

Sounds like it's part of a chrome extension

like image 44
Raynos Avatar answered Sep 21 '22 21:09

Raynos