最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Chaining a function in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

I want to make a function that add an item to my localStorage object. E.g.:

alert(localStorage.getItem('names').addItem('Bill').getItem('names'));

The first method is getItem which gets the item for localStorage objects... but addItem would be a custom function. This chain of functions would finally alert Bill.

So, how would I make this function chain to localStorage?

I want to make a function that add an item to my localStorage object. E.g.:

alert(localStorage.getItem('names').addItem('Bill').getItem('names'));

The first method is getItem which gets the item for localStorage objects... but addItem would be a custom function. This chain of functions would finally alert Bill.

So, how would I make this function chain to localStorage?

Share Improve this question edited Jul 6, 2010 at 0:15 gblazex 50.1k12 gold badges99 silver badges92 bronze badges asked Jul 4, 2010 at 22:26 Oscar GodsonOscar Godson 32.8k42 gold badges125 silver badges206 bronze badges 4
  • That doesn't make any sense. What would getItem return? – SLaks Commented Jul 4, 2010 at 22:29
  • getItem should be at the end of the chain, but aside from that it can be done – gblazex Commented Jul 4, 2010 at 23:42
  • Shouldn't it just be alert(localStorage.getItem('names').addItem('Bill')). – Lèse majesté Commented Jul 5, 2010 at 0:32
  • Yes, thatd work also if it returned all the names including Bill... – Oscar Godson Commented Jul 5, 2010 at 23:32
Add a ment  | 

4 Answers 4

Reset to default 5

This is possible if you create a wrapper/decorator object that makes chaining possible. This is how jQuery works for example. But it is useless in this case.

I made a possible implementation, though.

But getItem will break the chain (so it should always be used at the end).

storage().setItem('names', 'Bill').getItem('names'); // or
storage().setItem('names').addItem('Bill').getItem('names'); // or
storage().key('names').value('Bill').set().get('names');

Implementation

(function( window, undefined ) {

var storage = function( key, value ) {
  return new storage.init( key, value );
};

storage.init = function( key, value ) {
  this._key   = key;
  this._value = value;
};

storage.init.prototype = {

  key: function( key ) {
    this._key = key;
    return this;
  },

  value: function( value ) {
    this._value = value;
    return this;
  },

  get: function( key ) {
    key = isset(key) || this._key;
    return localStorage.getItem( key );
  },

  set: function( key, value ) {
    this._key   = key   = isset(key)   || this._key;
    this._value = value = isset(value) || this._value;
    if ( key && value ) {
      localStorage.setItem( key, value );
    }
    return this;
  },

  addItem: function( value ) {
    this._value = isset(value) || this._value;
    if ( this._key && value !== undefined ) {
      localStorage.setItem( this._key, value );
    }
    return this;
  },

  // aliases...
  getItem: function( key ) {
    return this.get( key );
  },
  setItem: function( key, value  ) {
    return this.set( key, value );
  }
};

function isset( value ) {
  return value !== undefined ? value : false;
}

window.storage = storage;

})( window );

This is impossible.

If getItem('names') returns Bill, you can't call addItem on it.
It would be possible to add addItem and getItem methods to every item in the storage, but it would be a horrible idea.

The simplest thing would be to not do this at all :)

If that doesn't work, then there are solutions like modifying the native getItem function but that's a really bad idea. The best way would be to write a wrapper around localStorage, much like what jQuery does to DOM nodes, and use that wrapper to get and put items in local storage. The wrapper could have an interface like:

ApplicationStorage
    getItem()
    setItem()
    key()
    removeItem()

Then define acceptable type wrappers like Array within ApplicationStorage that have methods like addItem

ApplicationStorage.Array
    addItem()

When applicationStorage.getItem("names") is called, return an object of ApplicationStorage.Array that contains the method addItem which will make chaining possible.

getItem('names').addItem('Bill').getItem('names')

I am confused, how do you get an item that has not been added???

getItem returns an object contains addItem method, and addItem method returns an object contains getItem method

Anyway, have a look the following tutorial. It does not pletely solve your problem, but will give you an idea.

http://guanfenglin.spaces.live./blog/cns!4E0822BF6C959B7F!668.entry

发布评论

评论列表(0)

  1. 暂无评论