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.

**

3
  • 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. Commented Aug 15, 2013 at 8: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. Commented Aug 15, 2013 at 8:26
  • See also this: stackoverflow.com/questions/17808651/return-function-javascript/… and this: stackoverflow.com/questions/17460556/… Commented Aug 15, 2013 at 8:28

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Another way to get the number of provided arguments is arguments.length. Maybe a nicer solution.
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

Comments

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"); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.