background preloader

Objects And Inheritance

Facebook Twitter

Using Prototypes in Javascript | TimKadlec.com. As mentioned in my previous post, I think using prototypes is powerful enough to deserve a more detailed explanation. To start off, let me say we are talking about the prototype method here, not the JavaScript library. Prototypes allow you to easily define methods to all instances of a particular object. The beauty is that the method is applied to the prototype, so it is only stored in the memory once, but every instance of the object has access to it. Let’s use the Pet object that we created in the previous post. In case you don’t remember it or didn’t read the article (please do) here is the object again: function Pet(name, species){ this.name = name; this.species = species;}function view(){ return this.name + " is a " + this.species + "! ";} Pet.prototype.view = view;var pet1 = new Pet('Gabriella', 'Dog'); alert(pet1.view()); As you can see, by using simply using prototype when we attached the view method, we have ensured that all Pet objects have access to the view method.

The prototype object of JavaScript. The prototype object of JavaScript No, we're not going to discuss how to construct a new version of JavaScript in this tutorial. The prototype object of JavaScript, introduced starting in JavaScript 1.1, is a prebuilt object that simplifies the process of adding custom properties/ methods to all instances of an object. I know, I'm starting to sound a little geeky already, but hay, JavaScript isn't just about fun and games...it's important to learn the serious side of it too. A little background first... In JavaScript, you're allowed to add custom properties to both prebuilt and custom objects. Here's an example of each: //adding a custom property to a prebuilt object var myimage=new Image() myimage.size="26k" /*adding a custom property to the custom object "circle"*/ //First, create the custom object "circle" function circle(){ } var smallcircle=new circle() smallcircle.pi=3.14159 A custom property added this way only exists for that instance of the object.

JavaScript Class Constructor vs. Object Literal: Difference in Implementation and Inheritance. There is a difference between object literal and what is called a "constructor" in JavaScript. For some reason a lot of people think that these two are interchangeable, but they are a totally different thing. Here is an example: This is most people refer to as object literal in JavaScript. It is an object that can be a accessed right away, with all its nodes, so when you want to call a method, it works like this: objLit.someMethod(); On the other side you have a simple constructor, which might look like this: var myConst = function(){ this.someOtherProperty = "some value"; this.someOtherMethod = function(){ console.log(this.someOtherProperty); } } Obviously, if you want to call the inner method directly, it will not work, because it is not an object yet. myConst. someOtherMethod(); First you have to create an "instance of a class:" var myInst = new myConst(); Now you you have your public methods play with the public methods: myInst. someOtherMethod(); var newLitInstance = objLit;

Cloning

Private Members in JavaScript. Douglas Crockford www.crockford.com JavaScript is the world's most misunderstood programming language. Some believe that it lacks the property of information hiding because objects cannot have private instance variables and methods. But this is a misunderstanding.

JavaScript objects can have private members. Objects JavaScript is fundamentally about objects. If a value is a function, we can consider it a method. Objects can be produced by constructors, which are functions which initialize objects. Public The members of an object are all public members. In the constructor This technique is usually used to initialize public instance variables.

Function Container(param) { this.member = param; } So, if we construct a new object var myContainer = new Container('abc'); then myContainer.member contains 'abc'. In the prototype This technique is usually used to add public methods. Container.prototype.stamp = function (string) { return this.member + string; } So, we can invoke the method Private Privileged.

Quick guide to somewhat advanced JavaScript. Last update: February 21st 2006 Hey, I didn't know you could do that If you are a web developer and come from the same place I do, you have probably used quite a bit of Javascript in your web pages, mostly as UI glue. Until recently, I knew that Javascript had more OO capabilities than I was employing, but I did not feel like I needed to use it. As the browsers started to support a more standardized featureset of Javascript and the DOM, it became viable to write more complex and functional code to run on the client. As we all start to learn what it takes to write our cool, AJAXy applications, we begin to notice that the Javascript we used to know was really just the tip of the iceberg. In many ways we can say that suddenly the bar was put much higher than before. The purpose of this article is precisely explaining the types of constructs that many of us are not familiar with yet. Related article Prototype.js documentation What do you mean?

Take a look at this example. Whoa! JavaScript Inheritance Part 2: The Explicit Object Construction Model - rhyolight's posterous.