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

jQuery and object-oriented JavaScript - howto? - Stack Overflow

programmeradmin5浏览0评论

I've read this and this (thanks google) But it doesn't help enough. I'd like to know, if, straight out of the box, without any plugin addons, it's possible to do something like it's possible with prototype, for example:

MyClass = Class.create(Table,
{
  cookieName: 'w_myclass',
  prefix: 'myclass',
    ...blabla...

  // function
  initStr: function()
  {
    ...blabla...
  },

  // another function
  getIndice: function(param)
  {
    ...blabla...
    return 0;
  }

});

Any idea/suggestions are wele.

I've read this and this (thanks google) But it doesn't help enough. I'd like to know, if, straight out of the box, without any plugin addons, it's possible to do something like it's possible with prototype, for example:

MyClass = Class.create(Table,
{
  cookieName: 'w_myclass',
  prefix: 'myclass',
    ...blabla...

  // function
  initStr: function()
  {
    ...blabla...
  },

  // another function
  getIndice: function(param)
  {
    ...blabla...
    return 0;
  }

});

Any idea/suggestions are wele.

Share Improve this question edited Jan 14, 2012 at 9:08 asked Jan 14, 2012 at 8:50 user292916user292916 3
  • 1 Why not use javascript's native object-orientation features? – Marcin Commented Jan 14, 2012 at 10:10
  • 1 JavaScript is about as "object-oriented" as it gets. You're asking about class-based inheritance, not OO-ness. – Tony R Commented Jan 14, 2012 at 10:16
  • there is no out-of-box solution. But there is another language coffeescript which piles one-to-one into the equivalent JS, so you can use features of coffescript with every js framework – Fivell Commented Jan 14, 2012 at 11:51
Add a ment  | 

3 Answers 3

Reset to default 6

JQuery never had the purpose of being a class framework. It's about page manipulation and tools (like AJAX). You can pound a nail with a fork, but why not use a hammer?

Using native JavaScript to create a class-based inheritance system is asking for trouble unless you're a highly skilled JavaScript programmer. Douglas Crockford will tell you it's possible, but he has a deep understanding if the intricacies of closure etc etc. Also, using native inheritance features bees unsustainable very quickly if your system grows large.

I highly remend James Coglan's JS.Class framework. The class definitions will look almost identical to your example. It's not native JS but it works fine with JQuery.

If you want a near object oriented solution using javascript with jquery you can define an object in javascript that will set up your event controllers.

The second half of this post http://www.codescream./?p=18 covers that. but i'll write here a resume on how to make an object in javascript that you can use in a near object oriented structure.

It would look something like this:

function myObject(constructorParam1, constructorParam2, anotherOne, blabla){

  var text = "";
  // this event will be set everyTime you run myObject
  $(".foo").click(function(){
    text = $(this).text(); // copies the text inside the elements ".foo" to a local variable
    doSomething();
  });

  function aPrivateFunction1(){


  }

  function aPrivateFunction2(){

  }

  function internalAdd(a,b){
    return a+b;
  }

  var size = 1; // privateVaribale
  var name = blabla;


  if(name===undefined){
     name="No name";
  }

  aPrivateFunction1(); // run "aPrivateFunction1()

  // you can consider all code above as being part of the constructor.
  // The variables declared above are private, and the functions are private as well

  // bellow are public functions  that you can access in an OOP manner
  return {
    getSize: function(){
        return size;
    },

    setSize: function(newSize){
        size = newSize;
    },

    getName: function(){
        return name;
    },

    setName: function(newName){ 
        name = newname;
    },

    addAndTurnPositive: function(n1,n2){
        var val = internalAdd(n1,n2);
        if(val<0){
            return val*-1;
        }
        return val;
    }
  }
}

// then you can run it like
var anInstance = myObject("aaa",1234,"asdak",123);
anInstance.setSize(1234);
var t = anInstance.addAndTurnPositive(-5,2);

In a word, no. jQuery doesn't offer any class and inheritance functionality. You'd need to include another library, such as klass (although there is a jQuery plugin which ties it in more closely with jQuery)

发布评论

评论列表(0)

  1. 暂无评论