1)首先是克莱德·洛博(Clyde Lobo)的回答:
var f = new foo();
f.bar();
2)在(特权)上写一个函数consturctor
,每个实例都会创建一个新的,如果在 上定义一个方法prototype
,每个实例共享相同prototype
的方法:
var f1 = new foo(), // instance 1
f2 = new foo(); // instance 2
f1.privileged === f2. privileged // -> false , every instance has different function
f1.bar === f2.bar // -> true, every instance share the some function
3)你可以调用this.bar()` bar2
,bar' by
代码如下:
function foo() {
this.property = "I'm a property";
this.privileged = function() {
// do stuff
};
}
foo.prototype.bar = function() { // defined a method bar
alert('bar called');
this.bar2(); // call bar2
};
foo.prototype.bar2 = function() { // defined a method bar2
alert('bar2 called');
};
var f = new foo();
f.bar(); // alert 'bar called' and 'bar2 called'
f.bar2(); // alert 'bar2 called'