javascript - Object cannot access JS constructor function -


i new js. learning js oop's concepts. trying use constructor create private variables in js. when try access values using getters, error 'typeerror: 'undefined' not function '

function card(n,s) {     var number = n;     var suit = s;     //getters     var getnumber = function(){             return this.number;     };     var getsuit = function(){             return this.suit;     }; } var test = new card(10, 1); test.getnumber(); 

i not able figure out error might be. need on this.

because did not attach functions this, points instance of constructor. properties , functions attached this "public" properties , methods in classical oop.

also, variables number , suit live inside instance not properties of instance. cannot accessed via this because didn't attach them. act "private variables" in classical oop.

but since getters in same scope (the constructor card) variables, have access them, "getters" , "setters" of classical oop.

function card(n, s) {   var number = n;   var suit = s;   //getters   this.getnumber = function () {     return number;   };   this.getsuit = function () {     return suit;   }; } var test = new card(10, 1); test.getnumber(); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -