所有的鸟儿他们都知道, 他们的巢应该筑在什么地方, 鸟儿知道自己该在什么地方筑巢, 那就意味着他们了解他们自己的使命。 我们身为万物之灵的人类, 怎么会不知道,连鸟儿都知道的道理呢?
Static and Instance Methods in JavaScript
Static and Instance Methods in JavaScript

Static and Instance Methods in JavaScript

原文:http://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-javascript/

 

 I thought I quite ‘understood’ inheritance in JavaScript until I got flummoxed while trying to test my understanding. The JS prototypical inheritance model is hugely different from the classical approaches of the languages I started out with; the only way to fix this that I know of is by writing code and after spending hours screaming at my console I finally saw the light Alhamdulilah.

JS is a weakly-typed prototypical language and thus classes aren’t really ‘classes’; instead they are functions which are in turn objects. New objects are created from constructor functions by using the new keyword and this allows you to kind of ‘simulate’ OOP. But mind you; its inheritance model is still different.

 Some sample code that shows this difference between static and instance properties. All object properties are public although can easily make them private by declaring them with var (I added an example); for these private properties you’ll have to add accessors and setters; read this for an explanation of closures and this pattern.

 

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
//Constructor
var Person = function (name, age){
//private properties
var priv = {};
 
//Public properties
this.name = name;
this.age = age;
 
//Public methods
this.sayHi = function(){
alert('hello');
}
}
 
// A static method; this method only
// exists on the class and doesn't exist
// on child objects
Person.sayName = function() {
alert("I am a Person object ;)");
};
 
// An instance method;
// All Person objects will have this method
Person.prototype.setName = function(nameIn) {
this.name = nameIn;
}
 
// Tests
var per = new Person('John Doe', 22);
 
//Shows alert
Person.sayName();
 
//TypeError: Object [object Object] has no method 'sayName'
per.sayName()
 
//Show alert
per.sayHi();
 
//John Doe
per.name;
 
//22
per.age;
 
per.setName('Jane Doe');
 
//Jane Doe
per.name;
view rawgistfile1.js hosted with ? by GitHub

 

It’s interesting to see that static fields cannot be accessed from child context in JavaScript (which makes sense); Java, however, allows static fields to be accessed from object context; this is kinda weird as the objects don’t really ‘have’ that variable.

Here’s a quote from the Java Tutorials website:

Note: You can also refer to static fields with an object reference like

myBike.numberOfBicycles

but this is discouraged because it does not make it clear that they are class variables.

One tip: don’t just read code and assume you understand it. Sure, you can always convince yourself that you ‘know’ it. However, if you really want to KNOW it then read it, write it and then try extend it (if you have the time and enthusiasm).

So, what other differences do you know between classical and prototypical inheritance?

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注