A real JavaScript keyword or just a bug in Notepad++? - javascript

I use Notepad++ for web-development. I was coding in JavaScript when I found this weird thing. I named a variable as private (I knew it wasn't a reserved keyword in JavaScript). But as I typed, it turned blue (all the JavaScript keywords are shown blue in Notepad++). So I thought there'll also be public, class and protected keywords. And to my surprise, they also turned blue! So does it mean JavaScript has class-based object-oriented model along with the prototypical model? I checked the following code -
public class Foo {
private bar;
}
But the console said - SyntaxError: Unexpected reserved word.
Question - Does JavaScript support class-based object-oriented model or is it just a bug in Notepad++ or my syntax is wrong?

It is a reserved keyword so that in future, this feature could be added without breaking existing code.
If you allowed private, then adding private feature to the language later would break any code that was using private as a normal identifier.

private and public are Java keywords and are reserved by JavaScript.
class is an ECMAScript reserved word.
For OO class based implementations in JavaScript, please see here

Related

How encapsulation works in TypeScript

I am trying to understand how encapsulation works in Typescript and come up with an example which makes me confusing why am I able to access and even change directly the private members of a certain class.
class Encapsulate {
str:string = "hello"
private str2:string = "world"
}
var obj = new Encapsulate();
console.log(obj.str); //accessible
obj.str2 = "something else";
console.log(obj.str2); //compilation Error as str2 is private
OUTPUT: hello something else
I am getting a compile time warning like compilation Error as str2 is private, but still I am able to change it or access it. Am I missing the concept of Encapsulation in general, what it is and how it works on Typescript.
The problem is that typeScript just compiles to javaScript, but it doesn't have a runtime.
TypeScript is giving you those warnings at compilation type. It is telling you:
Ey, Encapsulate str2 is private, and you're still trying to access it. You shouldn't do that.
However, once ts compiles and generates the js code, all the typescript annotations are lost in the code. JavaScript does not know anything about private, enum or interfaces. Once your code gets compiled and then runs in a browser or any other js runtime, you'll have an Encapsulate javaScript object with two fields, str1 and str2. They cannot be private or public because js does not know about that. Encapsulate will be a plain bare javaScript object.
So, basically, typeScript helps you catch errors at compile time. But, once the code compiles, it will 'forget' everything about typeScript annotations.
It can tell you that you shouldn't access str2. It can even refuse to compile if such an error is found (this depends upon tsconfig configurations). But the compiled code won't have anything to do with typescript.
This is expected as TypeScript is just a static type checking language.
Once compiled to js, the operation you are performing is valid, hence no runtime error
Javascript has no concept of private and public members for a class (though this might change in the future). At this time marking a property or function in a class as private is only checked at compile time (this is why you get a syntax error).
Your code will still run though because even though Typescript generated a syntax error it also most likely still outputted valid Javascript code.

Is there a `declare` keyword in ES6/7?

Recently I've been studying ES6, and that lead to me using Babel a lot. Being the curious type, I started looking at the Babel Github repository to know how they built this awesome tool, and know if I can somehow contribute.
However, I came across this file, and it has things like declare class BabelNodeSourceLocation {} written all over it, and the file ends with .js.
This got me very confused, and I am now wondering whether there's a declare keyword in JavaScript that I didn't know of, or is this just a Babel-specific syntax? All my Google searches resulted in nothing.
Update: Putting the code in the Babel REPL resulted in nothing. Babel just ignored the code and did not produce any equivalent ES5 output. It also did not throw any error.
and the file ends with .js.
That doesn't mean a lot these days :-)
I am wondering whether there's a declare keyword in JavaScript that I didn't know of
No, there is not.
Or is this just a Babel-specific syntax?
No. It is a type declaration file for the Flow typechecker.
With Flow, you can declare a global class that allows you to reference the class type anywhere in your project. This has no affect on runtime code and won't affect babel output.
An example from the docs:
declare class URL {
constructor(urlStr: string): URL;
toString(): string;
static compare(url1: URL, url2: URL): boolean;
};
And then in your project you can reference URL as a class type.
Similarly, you can declare other global Types, Modules, Functions, Variables. A good way to keep them organized.

how can I write an annotation for Dart

The question:
What is the procedure to implement an annotation.
How can, or indeed when can one activate the annotation you developed?
I can't seem to find an example or tutorial on how to write a class to implement annotations for dart.
For example with Java you might have an annotation that represents a class that is invoked at compile time and let you modify or inject code. Do Dart annotations work like this as well?
background
I have done some (further) digging on understanding this area of the Dart ecosystem. I'm adding some notes because annotation can be a powerful with transparent commentary on how to use it.
After looking at some actual annotation from Dart, Dart annotations record "a notation" (a label or metadata tags). The question is about how to uses annotations within Dart.
gloaming
My current understanding, based on looking at bits of code, is that they are markers on class objects. It looks like annotations are highly-unstructured since while an annotation can be declared simply, there's no structure to use or recognise a label (aka annotation).
steps of annotation
Identify the property or action you want to label.
Need to write code to use or 'work' your annotation. Look at something like Observe as an example.
You can implement and test LOAD-time code to look-for and process your labels. I don't see an infrastructure to register an annotation and provide handlers for example.
This is done via the main() method in your library.
Implement and test annotation behaviour.
At least I think that's how it works. There's not really a lot of information in the Dart language specification on this area.
Observation and inspection raised a few general questions as well. I've left a reading list of sorts and examples, to assist others in joining the exploration.
readings:
Dart Language Specification
type annotations
metadata
extending a class
I love Dart annotations
zones
zone class
Dart Annotations are No Longer Structured
examples:
Observe
Observable.dart
annotations.dart
Any class with a const constructor can be used as annotation.
const FOO = const Foo(37);
#Foo(42)
class Foo {
#Deprecated("until further notice");
final int x;
#FOO
const Foo(this.x);
}
There is nothing more to it.
See also https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#ch02-metadata
Metadata doesn't do anything by itself. If your program wants to read metadata off a class, it needs to use mirrors.
import 'dart:mirrors';
const tag = "TAG";
#tag class C {}
void main() {
print(reflectClass(C).metadata.first.reflectee); // prints "TAG"
var c = new C();
print(reflect(c).type.metadata.first.reflectee); // prints "TAG"
}
See: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-mirrors.ClassMirror#id_metadata
Alternatively, you can process the source directly. For example, the dart2js compiler has a "source mirror" library that reflects over the source structure. It is what dart2js and the analyzer do to understand the "proxy" annotation.

JavaScript: firefox refuses the class keyword while chrome doesn't

I was trying this JavaScript piece of code:
class User(name) {
this.name = name;
}
var class = new User("Kimo");
The above was used as is in a JavaScript book, and I was simply playing with it.
firefox 6.0.2 (from the console) refused the 'class' keyword as a variable name (complained about a syntax error), while chrome 13.0.782.220m (from the developer tool) didn't complain at all.
It makes me think, which one is right? Since the notion of a class in JavaScript doesn't exist or it is different than other OOP languages. On the other hand, it might be wiser to prevent developers from using it.
I would like to know why this different approaches between firefox and chrome (I know they use different engines).
Thanks
First of all you can`t create a class in Javascript like this. Either create an object:
var User = {...};
or a constructor function:
function User() {...}
class is reserved word from about.com (some not listed in ECMA 262, but present in JScript) and MDC - reserved for future use. It is true that JavaScript has no classes for the moment. But the word is reserved in the sense that some day it can have such.
Mozilla is more strict about the rules than other browsers and gives a syntax error.
In addition :
JavaScript is not purely Object Oriented Programming language ( OOP ) it is Prototype-based Programming language.
On the other hand, it might be wiser to prevent developers from using it.
As JavaScript is interpreted by the browser, I don`t think the usage of reserved words can be prohibited. In the compiled code you have the compiler to disagree with you about such things, before the product is ready to use.
One other thing - you can write JavaScript code on editor with no capabilities to tell you, that your code has errors, or that you use reserved words for variables.
An error is correct behavior. Class is a restricted keyword in ECMAScript.
Firefox didnt implement Class support yet, if you check ecma6 browser support aka javascript(its a new realease that will have more feactures such as class :D), you will see that Class for mozilla isnt possible, lets hope soon will be available, ecma 6 is in beta stage, so it will be a while till all browsers support all the feactures

JScript 'import' syntax for ASP classic

I'm using an ASP "classic" server, with JavaScript as the language engine, specified by the directive:
<%#LANGUAGE='JAVASCRIPT'%>
I've noticed that `import' seems to be a keyword.
Technical Information (for support personnel)
* Error Type:
Microsoft JScript compilation (0x800A03F2)
Expected identifier
/Default.asp, line 4, column 4
var import = 'whut'
---^
How is the `import' keyword used, if at all?
Like in most programming languages, certain keywords are reserved. Javascript is a bit special since it reserves more keywords than are implemented in the language. Import is one of those words. Other examples are 'private' and 'void', though they are not implemented in the current releases.
From here:
The import statement allows a script
to import properties, functions and
objects exported by a signed script.
The following code imports the 'wine'
and 'beer' properties of the object
'drinks' provided they have been made
available by an exporting script
(compare the export statement):
Code: import drinks.beer, drinks.wine;
NOTE: Any exported script must be
loaded into a window, frame or layer
before it can be imported and used.
import is a reserved word in js, but I think it is only actually used in JScript.NET and ActionScript.
The full list of reserved words for JScript (although many are not used in the language) are:
abstract, boolean, break, byte, case, catch, char, class, const, continue, debugger,
default, delete, do, double, else, enum, export, extends, false, final, finally,
float, for, function, goto, if, implements, import, in, instanceof, int, interface,
long, native, new, null, package, private, protected, public, return, short, static,
super, switch, synchronized, this, throw, throws, transient, true, try, typeof, var,
volatile, void, while, with
Hope this helps.

Categories

Resources