Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-03-2007, 05:04 PM   PM User | #1
never-online
New to the CF scene

 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
never-online is an unknown quantity at this point
a javascript defineSetter implement

look the codes as following or click a demo URL:
demo:http://www.never-online.net/code/js/...ineSetter.html
Code:
      function jsClass() {};
      jsClass.prototype = {
        value: 'never-online',
        URL: 'http://',
        blog: 'blog',
        name: "jsclass",
        println: function () {
          alert(this.value);
          alert(this.blog);
          alert(this.name);
          alert(this.URL);
        },
        write: function (value) {
          document.writeln(value);
        }
      };
      jsClass.defineAllSetter();
      jsClass.defineSetter('value',
        function (arg) {
          if ("a"===arg) return "Absolute a";
          return "not a";
        }
      )
      var o = new jsClass();
      o.setValue('a');
      o.setURL("<a href='http://www.never-online.net/blog'>http://www.never-online.net</a>");
      o.setName("never-online");
      o.setBlog("never-online's blog");
      o.println();
as you see, the jsClass do not has the setValue, setURL setName and etc. method to set the jsClass property, and i add a method to jsClass, make the class has set method with all property, that is defineAllSetter()

how to implement defineAllSetter method?

we know the js has feature which is dynamic to add method
the principle code like this:

Code:
var o = { property:'i am a property' }
o['printProperty']=function () {
 alert(o.property);
}
and the all code with defineSetter as following:
Code:
var _defineSetter = function (self, p, hdle) {
  if (!p || !self || "function"!=typeof self) 
  return null; p = String(p); self = self.prototype;
  if (p && "function"!=typeof self[p]) {
    var n = (function _n (n) {
      n = String(n); for (var i=0; i<n.length; i++) {
        var letter = n.substring(0,1).toUpperCase();
        n = n.substr(1);
        if (/[a-z]/i.test(letter))
        return 'set'+letter+n;
      }; return null;
    }) (p);
    if (n && hdle && "function"==typeof hdle)
      self[n] = new Function("v", "this." +p+ "=" +hdle+ ".apply(this,[v]);");
    else
      self[n] = new Function("v", "this." +p+ "=v;");
    return self[n];
  }; return self;
};

Function.prototype.defineAllSetter = function (hdle) {
  for (var i in this.prototype)
  _defineSetter.apply(this, [this,i,hdle]);
  return this;

};

Function.prototype.defineSetter = function (p, hdle) {
  return _defineSetter.apply(this, 
  [this].concat(Array.prototype.slice.call(arguments,0)));
};
the finished demo URL is
http://www.never-online.net/code/js/defineSetter/
never-online is offline   Reply With Quote
Old 05-03-2007, 05:40 PM   PM User | #2
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
Quote:
how to implement defineAllSetter method?
Are you asking a rhetorical question? Or does something not work?

Maybe you meant to post it here?

http://www.codingforums.com/forumdisplay.php?f=19

david_kw
__________________

www.eXfer.net
david_kw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:19 PM.


Advertisement
Log in to turn off these ads.