0

I wish to do something like this:

function Student (id, class) {
  var id = id
  var class = class

  this.get = function (subject) {
    $.ajax({
      url: 'myurl',
      data: { id: id, class: class, subject: subject },
      success: function (r) { return r }
    })
  }
  this.set = function (subject, mark) {
    $.ajax({
      url: 'myurl',
      method: 'post',
      data: { id: id, class: class, subject: subject, mark: mark },
      success: function (r) { return r }
    })
  }
}

my question is how can I modify my function so that I can create new student as below

var s1 = new Student (22, 4) // to set predefined id & class

but, I want the set and get as below (like jquery set & get)

s1("math") // to get 
s1("history", 70) // to set

**

so i think the answer is not possible to work as an object to store attribute id & class and call like a function without function name. thanks for your answer guys.

**

yskeat
  • 17
  • 1
  • 5
  • You can't call an object like a function (unlike in PHP): `s1("math")`. This will cause `Uncaught TypeError: object is not a function` being triggered. Please see my answer for further suggestions. – ComFreek Aug 15 '13 at 08:24
  • 1
    You can't return anything via ajax. You need to pass code that you want to execute after the ajax call as callbacks instead. – slebetman Aug 15 '13 at 08:26
  • See also this: http://stackoverflow.com/questions/17808651/return-function-javascript/17810720#17810720 and this: http://stackoverflow.com/questions/17460556/undefined-return-value-from-the-function-call-javascritpt/17460802#17460802 – slebetman Aug 15 '13 at 08:28

3 Answers3

2
  1. You can check how many arguments the caller has provided. Or check for undefined values.

    function test(a, b) {
      // both ifs check b was not provided
      if (typeof b === "undefined") {
      }
      if (arguments.length == 1) {
      }
    }
    
  2. Your current functions probably won't work because you are returning from a callback. AJAX is (in most cases) asynchronous. So in your case, you have to add another argument for providing a callback.

    this.get = function (subject, callback) {
      $.ajax({
        url: 'myurl',
        data: { id: id, class: class, subject: subject },
        success: function (r) { callback(r); }
      })
    }
    

FYI, class is a reserved keyword by the ECMAScript specification.

ComFreek
  • 29,044
  • 18
  • 104
  • 156
0
function sample(x,y){
id=x;
subjectClass =y;
if(arguments.length == 1){
//getter
}
else{
//setter
}
}

call getter

sample("maths")

call setter

sample("history",70);

Note :

class is a reserved keyword, so please remove it and you can use some other variable name

Amith
  • 1,424
  • 1
  • 10
  • 22
0

but, I want the set and get as below

That would mean s1 would be a function, not a Student instance. So your constructor would need to return that.

function student(id, klass) {
    // no need to declare variables here that are parameters already
    return function(subject, mark) {
        var data = {id: id, class: klass, subject: subject},
            opt = {url: 'myurl', data: data};
        if (arguments.length > 1) { // something was given to `mark`
            data.mark = mark;
            opt.method = "post";
        }
        return $.ajax(opt);
    };
}

Btw, since you cannot return the response from an ajax call, the function will return the jqXHR promise:

var s1 = student(22, 4); // `new` is unnecessary now
s1("math").then(function(r) { console.log("got maths result:", r); });
s1("history", 70).then(function(r) { console.log("successfully set marks"); });
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375