javascript - Whats the equivalent of ES6 methods(class) in es5? -


how polyfill es6 class methods es5?

i reading book , says following:

class ninja {     constructor(name) {         this.name = name;     }      swingsword() {         return true;     } } 

is same

function ninja(name) {     this.name = name; }   ninja.prototype.swingsword = function() {     return true; }; 

i asking why adding swingsword on prototype , not inside constructor function?

because function should on object , not on prototype chain.

am right or wrong?

it should on prototype, methods not per-instance data. can't think of language implements way, whole idea of classes have whole class of objects have same set of methods.

if put inside constructor function, unique function per instance made constructor. e.g, 1000 objects == 1000 functions, per "method".


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -