-2

How can I create an class object in jquery? In java or C/C++, I can do

class People {
   String name;
   public People(name) { this.name = name;}
   public String getName( return this.name; }
}

How can I convert the snippet code above to jquery or javascript?

WhatAName
  • 53
  • 3
  • 8
  • possible duplicate of [What's the best way to define a class in JavaScript?](http://stackoverflow.com/questions/387707/whats-the-best-way-to-define-a-class-in-javascript) – JJJ Nov 08 '14 at 07:08
  • 1
    By the way, jQuery has nothing to do with classes. It's just a JavaScript library and as such can't add basic features like classes to the language. – JJJ Nov 08 '14 at 07:09
  • The following may help as well: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Nov 08 '14 at 12:22

3 Answers3

0

I believe you would only need to use a getter method for a private variable. You can define a constructor and instantiate new objects with the 'new' operator. If you want to create a private variable, use 'var' to define it. For public values, set them as instance properties using:

this.PROPERTY = VALUE

Here is my fiddle: http://jsfiddle.net/nhmaggiej/avvtpcyc/

mags
  • 590
  • 1
  • 8
  • 25
0

for example:

function People(name){
      this.name=name;
      People.prototype.getName = function () {
         return this.name;
      }; 
}
var a=new People("a");

More about OOP of javascript, you can read this

sTg
  • 4,313
  • 16
  • 68
  • 115
  • 1
    Why would you re define prototype every time you create an instance? http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Nov 08 '14 at 12:22
-1

There are no classes in JavaScript. There are only objects (which are maps with String keys), which properties ('members') can be functions, allowing you to emulate methods; objects can inherit from other objects through a mechanism that is known as prototypal inheritance.

Valentin Waeselynck
  • 5,950
  • 26
  • 43