JSFiddle - React, Tailwind, and code Playground
by btipling
JavaScript
var Stack = (function() {
var privilegedInstanceMethods = {
push: {
value: function(x) {
this.arr.push(x);
console.log(this.foo);
// uncomment the following line to see that this
// is passed correctly when called from the prototype
// console.log("this: ", this, "arr: ", this.arr)
},
pop: function() {
return this.arr.length ? this.arr.splice(this.arr.length - 1, 1)[0] : null;
},
size: function() {
return this.arr.length;
},
empty: function() {
return this.arr.length === 0;
},
print: function() {
console.log(this.arr);
},
get: function() {
console.log(this.foo);
return this.arr;
},
};
var Stack = function () { // shadows outer Stack
var privateArr = [];
this.arr = Object.create(privateInstanceMembers, {
arr: {
get: function() {
return privateArr;
},
},
});
};
return Stack;
}());
// works
var s1 = new Stack();
var s2 = new Stack();
s1.arr.push("s1a");
s1.arr.push("s1b");
s2.arr.push("s2a");
s2.arr.push("s2b");
s2.arr.print();
s1.arr.print();
s1.getStuff();