How to classify class, function and function property in javascript? -


i've 3 objects these:

class person {     constructor(name, age) {         this._name = name;         this._age = age;     } }  let add = function (a, b) {     return + b };  let math = {     multiply: function (x, y) {         return x * y     } }; 

as can see, person class, add , math.multiply functions.

my question: how classify class , functions in case?

my question comes from: person looks function, this:

let person = function (name, age) {     this._name = name;     this._age = age; }; 

also, there no problems if declare object new instance of add function:

let result = new add(2, 3); // allowed although needn't 

new operator says:

the new operator creates instance of user-defined object type or of 1 of built-in object types has constructor function.

all of 3 cases, person, add , math.multiply constructors. so:

let person = new person('hermione', 18); // allowed let result = new add(2, 3); // allowed result = new math.multiply(2, 2); // allowed 

then, because of them constructors, it's difficult classify class , function (for me).

is there way achieve that?

i think question stems quite understandable confusion: despite class syntax, javascript doesn't have classes distinct functions class-based oop languages (java, c#, c++). class syntax creates function , associated properties on object referenced function's prototype property. it's not separate thing. see below bar more.

further, javascript didn't make distinction between normal functions , constructor functions; purely matter of how used used them (e.g., via new or calling them directly). of es2015 introduced both class syntax , arrow functions, language moving toward distinguishing different kinds of functions (the ones created via class throw error if call them without new; arrow functions throw error if do call them via new).

if goal @ person in code , know whether created class or function without calling it, way convert string , @ result. specification requires function.prototype.tostring return string that...

...must have syntax of functiondeclaration, functionexpression, generatordeclaration, generatorexpression, classdeclaration, classexpression, arrowfunction, methoddefinition, or generatormethod depending upon actual characteristics of object.

so instance, if i've used class foo { }, foo.tostring() has return class declaration or expression, whereas if i've used function foo { } instead, must return function declaration or expression. work out regular expression.

in comment you've asked zerkms

but in javascript can use add , math.multiply classes. better if can use function class? no, isn't.

it depends on how add , x.multiply (i wouldn't shadow built-in math object) defined: may make sense call them via new. it's not uncommon object property reference constructor function:

let nifty = {     stuff: class {         constructor(name) {             this.name = name;         }     } }; let x = new nifty.stuff("coolness"); console.log(x.name); // "coolness" 

or

// better not this, it's allowed let nifty = {     stuff: function(name) {         this.name = name;     } }; let x = new nifty.stuff("coolness"); console.log(x.name); // "coolness" 

re class creating functions: that's literally does:

class foo {  }  console.log(typeof foo); // "function"

in fact, this:

class foo { } 

roughly translates to

let foo = function() {     if (!(this instanceof foo)) { // check isn't quite right, it's close         throw new typeerror("class constructor foo cannot invoked without 'new'");     } }; 

and

class foo {     constructor(name) {         this.name = name;     }     sayhello() {         console.log(`hi, name ${name}!`);     } } 

roughly translates to

let foo = function(name) {     if (!(this instanceof foo)) { // check isn't quite right, it's close         throw new typeerror("class constructor foo cannot invoked without 'new'");     }     this.name = name; };  foo.prototype.sayhello = function sayhello() {         console.log("hi, name " + name + "!");     } }; 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -