从功能声明的堆栈访问变量

时间:2018-09-11 22:50:23

标签: javascript object data-structures stack

在使用JavaScript创建用于Stack的接口时-使用与原型或伪古典风格相反的功能风格,下面的“ storage”和“ count”变量应分别为someInstance.storage和someInstance.count,这样它们就可以稍后在此堆栈的实例上访问?

使用现在声明的变量,一旦我们通过执行函数创建堆栈实例,就无法访​​问计数(大小)和存储量(堆栈中的属性)。

声明我们以后需要使用功能创建模式访问的属性(而不是方法)的最佳方法是什么?

谢谢!

var Stack = function(){
  var someInstance = {};

  var storage = {};
  var count = 0;


  someInstance.push = function(value){
    storage[count++] = value;
  };

  someInstance.pop = function(){

    if(count){
      var popped = storage[--count];
    }

    delete storage[count];

    return popped;

  };

  someInstance.size = function(){
    return count;

  };

  return someInstance;
};

var stack = Stack();

5 个答案:

答案 0 :(得分:1)

可以在closure的javascript中研究以下提到的私有变量。

参考:https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Closures

var Stack = function(){
  // PUBLIC
  var someInstance = {};

  // PRIVATE: Attach _ to private variables. (just implicit rule.)
  var _storage = {};
  var _count = 0;


  someInstance.push = function(value){
    storage[_count++] = value;
  };

  someInstance.pop = function(){

    if(_count){
      var popped = storage[--_count];
    }

    delete storage[_count];

    return popped;

  };

  someInstance._size = function(){
    return _count;

  };

  // if you assign public variable, you can access!!!
  someInstance.size = _size;
  someInstance.count = _count;

  return someInstance;
};

var stack = Stack();

答案 1 :(得分:1)

storagecount 是否应为someInstance.storagesomeInstance.count取决于您希望该实例的用户如何工作。这有点基于意见。我的观点是,堆栈不应该允许用户使用someInstance.count = 10之类的值来更改计数,因为那样会破坏计数。

我个人将countstorage设为私有,这可以防止直接访问可能发生的意外错误。一种不错的方法是将它们捕获在一个闭包中,并为count提供一个吸气剂,这样您仍然可以读取计数,但不能更改它。像这样:

var Stack = function(){ 
    let storage = [], count = 0; // these will be caputured as a closure

    return {
        // the returned object will have push, pop, and a count getter
        push(v) {
            storage.push(v)
            count++
        },
        pop() {
            if (count) count--
            return storage.pop()
        },
        get count(){      // allow reading of count
            return count
        }

    } 
}
var stack =  Stack();
stack.push("hello");
stack.push("goodby");
stack.count = 10          // has no effect

console.log(stack.count)  // count is still 2
console.log(stack.pop())

console.log(stack.count)
console.log(stack.pop())

console.log(stack.count)

当然,由于这主要使用数组,因此您可以完全取消使用count变量,而只需使用storage.length

答案 2 :(得分:1)

您可以在someInstance对象(由闭包提供)中为其添加吸气剂和吸气剂,如下所示:

Object.defineProperty(someInstance, "count", {
   get: function() {
      return count;
   },
   set: function(value) {
      count = value;
   }
});

如果希望仅获取值而不设置值,则可以省略set部分。

示例:

var Stack = function(){
  var someInstance = {};

  var storage = {};
  var count = 0;

  Object.defineProperty(someInstance, "count", {
    get: function() {
      console.log("getter called");
      return count;
    },
    set: function(value) {
      console.log("setter called");
      count = value;
    }
  });

  return someInstance;
};

var stack = Stack();

console.log(stack.count);
stack.count = 55;
console.log(stack.count);

答案 3 :(得分:0)

var Stack = function(){ 
    let storage = [], count = 0; // these will be caputured as a closure

    return {
        // the returned object will have push, pop, and a count getter
        push(v) {  //method
            storage.push(v)
            count++
        },
        pop: function() {
            if (count) count--
            return storage.pop()
        },
        get count(){      // allow reading of count
            return count
        },
		get insidebox(){      // allow reading of count
            return storage
        },
		callprice:function(cnt){		 
		var price=cnt*100;
		return price;
		}

    } 
}


Stack.prototype.price2 = function() {
    return "testpr";
};

var sb =  Stack();
console.log(sb.count);
sb.push('paint');
console.log(sb.insidebox);
console.log(sb.count); 
sb.push('Sharee');
console.log(sb.insidebox);
sb.push('Shirt'); 
sb.push('shoes');
console.log(sb.insidebox);
sb.pop();
console.log(sb.insidebox);
console.log(sb.count);
console.log(sb.callprice(sb.count));

答案 4 :(得分:0)

function Person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}

Person.prototype.nationlity='Indian';

Person.prototype.fullname = function() {
    return this.firstName + " " + this.lastName
};

var myFather = new Person("Lalji", "Maurya", 50, "blue");
console.log(myFather.fullname());
console.log(myFather); 
console.log(myFather.nationlity); 

var myBrother = new Person("Ashish", "Maurya", 28, "black");
console.log(myBrother.fullname());
console.log(myBrother); 
console.log(myBrother.nationlity);