1

hi i want to know how to create objects in both javascript and jquery.

something like this.

Person = new Person();
Person.name = "John Doe";
Person.age = "16";

It is possible? Any help will do. Thank you

update #follow-up

If i'm able to create an object using javascript can i pass the object i recently created to a ajax request in jquery?

2
  • Why would you want to specify the age as a string? Commented Mar 5, 2012 at 7:47
  • oh. it is just an example. :D Commented Mar 5, 2012 at 7:55

5 Answers 5

4

jQuery is made using (or is) JavaScript, you can do this to work in both:

function Person(){
  this.name = '';
  this.age = 0;
}

Person = new Person();
Person.name = "John Doe";
Person.age = "16";

console.log(Person.name); // John Doe
console.log(Person.age);  // 16

You can pass arguments to constructor function as well:

function Person(name, age){
  this.name = name;
  this.age = age;
}

And add methods inside constructor or prototype too (I prefer latter):

function Person(name, age){
  this.name = name;
  this.age = age;

  this.getName = function(){
     console.log(this.name);
  };

  this.getAge = function(){
     console.log(this.age);
  };

}

Or better using prototype:

Person.prototype.getName = function(){
   console.log(this.name);
};

Person.prototype.getAge = function(){
   console.log(this.age);
};

Now you can create as many instances as you want:

var person1 = new Person();
var person2 = new Person();
person1.name = "John Doe";
person1.age = "16";    
person2.name = "Michael John";
person2.age = "25";    
person1.getName();
person1.getAge();
person2.getName();
person2.getAge();

Result:

John Doe
16
Michael John
25

Good Practices:

  • Always capitalize the first letter of constructor function (as in Person) so that people could know that it has to be instantiated with new keyword.

  • Generally/mostly it is good idea to add methods via prototype property (as shown above).

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

2 Comments

If i'm able to create an object using javascript can i pass the object i recently created to a ajax request in jquery?
@Dreyfus15: Yes you can do that. If you know how to use that passed object then fine otherwise that sounds a different question you can ask eg how to use passed objects in ajax request :)
2

It is possible. But not recommended. Look at this link which explains the what, the how and the why.

Comments

2

jQuery is just a Javascript library. jQuery doesn't have any objects in itself, you use the objects in Javascript. (jQuery does however have a few methods that can be useful for dealing with objects, like the extend method.)

You need a constructor function to use the new keyword. It's basically just a regular function:

function Person() {
}

Now you can create the object exactly as in your code.


You can also set the properties in the constructor:

function Person(name, age) {
  this.name = name;
  this.age = age;
}

Usage:

var person = new Person("John Doe", "16");

The constructor function has a prototype object that you can attach methods to:

Person.prototype.getFirstName = function() {
  return this.name.split(' ')[0];
};

Now you can use those methods on any object that you create with that function:

var person = new Person("John Doe", "16");
var firstName = person.getFirstName();

If you just need one object, for example to pass on some data, you can create it using the literal object syntax:

var person = { name: "John Doe", age: "16" };

Update:

Yes, you can create an object any way you like and pass it on to a jQuery method:

$.get(url, new Person("John Doe", "16"));

Comments

0

Something like this?

var person = {
  name: "John Doe",
  age: 16
};

Comments

0

Same as how you would do in Java

Car model=new Car();
model.number="21EX";
model.name="Mazda 21";

You could also create your own class in JavaScript as follows:-

 function People(){
//private properties
var _name = '';
var _age  = 0;

//private method
function doSomeThing(){}


//public method
this.setName = function(name){
    doSomeThing();
    _name = name;
};

 this.setAge = function(age){
    _age = age
 };
}

People person=new People();
 person.setName("John Doe");
 person.setAge(33);

Here is a quick example of the OOP JavasScript principles and how to use them. I also recommended reading about closures

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.