Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef in javascript

Tags:

javascript

Is it possible to typedef things in javascript somehow? Maybe by getting it from the prototype object or something?

For instance, i'd like to typedef the var keyword.

var string = prototype.var;

So now in stead of using 'var' i can use:

string blaat = "It's like using the 'var' keyword";

Is this possible somehow in javascript?

like image 829
Vivendi Avatar asked Mar 17 '12 19:03

Vivendi


People also ask

Why should I use JSDoc?

JsDoc provides two types of tags: block tags and inline tags. Block tags provide means to annotate variables, functions, arguments, modules, namespaces, return types, and classes and describe what a specific code snippet does. These could be types, arguments, callbacks, modules, namespaces, etc.

Can I use JSDoc in TypeScript?

You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.

What is JSDoc comment?

JSDoc comments are used for documentation lookup with Ctrl+Q in JavaScript and TypeScript, see JavaScript documentation look-up and TypeScript documentation look-up, as well as for type annotations and method return type hints in chained methods.

What is a typedef in C++?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.


1 Answers

No, this is not possible. Period.

Your example is also an impossible thing to want, even though I understand the motivation. JavaScript is dynamically typed. You cannot declare variables to be string. And in that light the whole statement string x = "foo"; is pointless.

EDIT Yes, it's possible to achieve this effect with TypeScript. No, TypeScript is not JavaScript. The question was about the latter. That you can do a similar thing in a completely different programming language does not make this answer incorrect or obsolete.

Declaring a variable as, e.g., string will remain impossible in JavaScript until the day when the ECMAScript Standard adds static typing to the language.

like image 92
Tomalak Avatar answered Sep 29 '22 10:09

Tomalak