admin管理员组

文章数量:1294131

I'm wondering if there is a way that Javascript can mimic an object oriented language. I.e. can you mimic the ability to define customized classes/objects with properties? Since JSON is basically a way of javascript passing objects, I figure there must be some way to create/store objects on the client side, then reuse them? If someone can point me to a good tutorial (if it's possible to do this at all) that would be much appreciated.

Even some example code where you can dynamically create an object, then consume it using jquery would be appreciated.

I'm wondering if there is a way that Javascript can mimic an object oriented language. I.e. can you mimic the ability to define customized classes/objects with properties? Since JSON is basically a way of javascript passing objects, I figure there must be some way to create/store objects on the client side, then reuse them? If someone can point me to a good tutorial (if it's possible to do this at all) that would be much appreciated.

Even some example code where you can dynamically create an object, then consume it using jquery would be appreciated.

Share Improve this question edited May 11, 2011 at 20:29 locoboy asked May 11, 2011 at 20:23 locoboylocoboy 39k73 gold badges188 silver badges263 bronze badges 1
  • 2 It doesn't need to mimic, it is. – Kelly Commented May 11, 2011 at 20:28
Add a ment  | 

8 Answers 8

Reset to default 5

Javascript is an object oriented language, it's just not a class based object system, but a prototype based one. See this article: http://en.wikipedia/wiki/Prototype-based_programming

I found this to be a great tutorial: https://developer.mozilla/en/Introduction_to_Object-Oriented_JavaScript

It must be said that Javascript is object oriented, however it is prototype based, rather than class based.

Douglas Crockford is a recognized person in JavaScript munity,

His talks about JS are really good. In this series he talks about advanced js including object oriented javascript.

http://video.yahoo./video/play?vid=111585 http://video.yahoo./video/play?vid=111586 http://video.yahoo./video/play?vid=111587

Javascript is an Object Oriented language.

JavaScript is object oriented. It's just that it's prototype-based rather than class-based.

That said, JSON is not really suitable for serializing objects with inherited properties.

The short answer is "yes". There are a couple of excellent articles at developer.mozilla:

  • A re-introduction to JavaScript which includes its relationship to object-oriented languages
  • Introduction to Object-Oriented JavaScript

If you prefer an actual book, this has been very useful for me: Pro Javascript Design Patterns

Although Javascript does not have a built-in class system (although the next version of Javascript might include this), there are many different Javascript OO Design Patterns that mimic the behavior of Classes. The Module pattern mimic's classes by allowing a Javascript object to hold both private and public members. This is achieved like so:

var Person = function() {
    var firstName = "Greg";
    var lastName = "Franko";
    return {
        getFirstName: function() {
            return firstName;
        },
        getLastName: function() {
            return lastName;
        }
    }        
}

//Instantiate a new Person object
var greg = new Person();

//This will not work because the firstName property is private
console.log(greg.firstName);

//This will work because the getFirstName method is public
console.log(greg.getFirstName());

本文标签: oopIs there a way that Javascript can mimic an object oriented languageStack Overflow