本文主要是介绍深刻理解JavaScript基于原型的面向对象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
createObj.js
oldObject={name:"Andy"};
oldObject.func = function () {
console.log("这是一个函数");
};
function create(oldObject)
{
function F(){};
F.prototype = oldObject;
var newObject = new F();
return newObject;
}
oldObject.func();
/*
function F(){};
F.prototype = oldObject;
var newObject = new F();
*/
var newObject =create(oldObject);
newObject.func();
console.log(newObject.name);
console.log(oldObject.name);
newObject.name="Lucy";
console.log(newObject.name);
console.log(oldObject.name);
newObject.func = function () {
console.log("这是另一个函数");
};
newObject.func();
oldObject.func();
"D:\Program Files\JetBrains\WebStorm 8.0.6\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" createObj.js
这是一个函数
这是一个函数
Andy
Andy
Lucy
Andy
这是另一个函数
这是一个函数
Process finished with exit code 0
closurePrivate.js
/*****************************************
. * 模拟私有变量
. *****************************************/
function Employee(_name_) {
//私有变量
var name = _name_;
this.getName = function() {
return name
};
}
var employee = new Employee('Jack');
console.log(employee.name);//undefined
console.log(employee.getName());//Jack
"D:\Program Files\JetBrains\WebStorm 8.0.6\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" closurePrivate.js
undefined
Jack
Process finished with exit code 0
newTry.js
function Employee(name){
this.name = name;
this.getName = function(){return this.name};
}
var employee = new Employee('Jack');
console.log(employee.name);
console.log(employee.getName());
"D:\Program Files\JetBrains\WebStorm 8.0.6\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" newTry.js
Jack
Jack
Process finished with exit code 0
深刻理解JavaScript基于原型的面向对象
- 博客分类:
- JavaScript
主题一、原型
- newObject = create(oldObject);
- function F(){};
- F.prototype = oldObject;
- var newObject = new F();
- class Empolyee{
- String name;
- public Employee(String name){
- this.name = name;
- }
- public getName(){
- return this.name;
- }
- }
- class Coder extends Employee {
- String language;
- public Coder(name,language){
- super(name);
- this.language = language;
- }
- public getLanguage(){
- return this.language;
- }
- }
class Empolyee{String name;public Employee(String name){this.name = name;}public getName(){return this.name;}}class Coder extends Employee {String language;public Coder(name,language){super(name);this.language = language;}public getLanguage(){return this.language;}}
- function Empolyee(name){
- this.name = name;
- }
- function Empolyee(name){
- this.name = name;
- }
- var employee = {};
- Employee.call(employee,'Jack');
- function Empolyee(name){
- this.name = name;
- this.getName = function(){return this.name};
- }
- function Coder extends Employee(name,language){
- super(name);
- this.language = language;
- }
- var topObject = {
- __version__ : 1.0;
- };
- function Empolyee(name){
- this.name = name;
- this.getName = function(){return this.name};
- }
- var employee = {};
- employee.__proto__ = topObject;
- Employee.call(employee,'Jack');
- function Coder(name,language){
- this.name = name;
- this.language = this.language;
- this.getLanguage = function(){return this.language};
- }
- var coder = {};
- coder.__proto__ = employee;
- Coder.call(coder,'Coder Jack','Java');
- function sliceArguments(argumentsObj,n){
- var args = [];
- for(var i=0;i<argumentsObj.length;i++){
- if(i>=n){
- args.push(argumentsObj[i]);
- }
- }
- }
- function newInstance(prototype,constructor){
- var obj = {};
- obj.__proto__ = prototype;
- constructor.apply(obj,sliceArguments(arguments,2));
- }
- var employee = newInstance(topObject,Employee,'Jack');
- var coder = newInstance(employee,Coder,'Coder Jack','Java');
- function newInstance(constructor){
- var obj = {};
- obj.__proto__ = constructor.prototype;
- constructor.apply(obj,sliceArguments(arguments,1));
- return obj;
- }
- function Employee(name){
- this.name = name;
- this.getName = function(){return this.name};
- }
- var employee = newInstance(Empolyee,'Jack');
- var employee2 = newInstance(Empolyee,'Jack2');
- var employee3 = newInstance(Empolyee,'Jack3');
- function Coder(name,language){
- this.name = name;
- this.language = language;
- this.getLanguage = function(){return this.language};
- }
- Coder.prototype = newInstance(Empolyee,'');
- var coder = newInstance(Coder,'Coder Jack','Java');
- var coder2 = newInstance(Coder,'Coder Lee','C#');
- var coder3 = newInstance(Coder,'Coder Liu','C++');
- var coder4 = newInstance(Coder,'Coder Liu','JavaScript');
- function Employee(name){
- this.name = name;
- this.getName = function(){return this.name};
- }
- var employee = new Employee('Jack');
- function newInstance(constructor){
- var obj = {};
- obj.__proto__ = constructor.prototype;
- constructor.call(obj,sliceArguments(arguments,1));
- return obj;
- }
- {
- __proto__:Object.prototype,
- constructor: 指向函数本身
- }
- f.prototype.constructor=f
- function Employee(){};
- function Coder(){};
- Coder.prototype = new Employee();
- Coder.prototype.constructor = Coder;
- var coder = new Coder();
- coder instanceOf Coder//true
- Coder.prototype.isPrototypeOf(coder)//true
- <script>
- function Employee(name){
- this.name = name;
- //this.getName = function(){return this.name};方法代码应该放到原型对象之中,而不是初始化函数中,这样每个employee对象都共享同一个方法代码
- }
- Employee.prototype.getName = function(){return this.name};
- var employee = new Employee('Jack');
- console.log("employee.getName(): " + employee.getName());//Jack
- var employee2 = new Employee('Jack2');
- console.log("employee2.getName(): " + employee2.getName());//Jack2
- function Coder(name,language){
- this.name = name;
- this.language = language;
- //this.getLanguage = function(){return this.language}; 方法代码应该放到原型对象之中,而不是初始化函数中,这样才能实现代码共享
- }
- Coder.prototype = new Employee('');
- Coder.prototype.constructor = Coder;//这一句话其实也可以不写,不影响继承
- Coder.prototype.getLanguage = function(){return this.language};
- var coder = new Coder('Coder Jack','Java');
- console.log("coder.getName(): " + coder.getName());//Coder Jack
- console.log("coder.getLanguage(): "+coder.getLanguage());//Java
- var coder2 = new Coder('Coder Lee','C#');
- console.log("coder2.getName(): " + coder2.getName());//Coder Lee
- console.log("coder2.getLanguage(): " + coder2.getLanguage());//C#
- var coder3 = new Coder('Coder Liu','C++');
- console.log("coder3.getLanguage(): " + coder3.getName());//Coder Liu
- console.log("coder3.getLanguage()" + coder3.getLanguage());//C++
- console.log("employee.constructor: " + employee.constructor);
- console.log("employee.constructor.prototype === Employee.prototype: " + (employee.constructor.prototype === Employee.prototype));
- console.log("employee.constructor.prototype.constructor === Employee: " + (employee.constructor.prototype.constructor === Employee));
- console.log("employee instanceof Object: " + (employee instanceof Object));
- console.log("employee instanceof Function: " + (employee instanceof Function));
- console.log("employee instanceof Employee: " + (employee instanceof Employee ));
- console.log("Employee.prototype.isPrototypeOf(employee): " + (Employee.prototype.isPrototypeOf(employee)));
- console.log("Function.prototype.isPrototypeOf(employee): " + (Function.prototype.isPrototypeOf(employee)));
- console.log("Object.prototype.isPrototypeOf(employee): " + (Object.prototype.isPrototypeOf(employee)));
- console.log("coder.constructor: " + coder.constructor);
- console.log("coder instanceof Object: " + (coder instanceof Object));
- console.log("coder instanceof Function: " + (coder instanceof Function));
- console.log("coder instanceof Employee: " + (coder instanceof Employee ));
- console.log("coder instanceof Coder: " + (coder instanceof Coder ));
- console.log("Employee.prototype.isPrototypeOf(coder): " + (Employee.prototype.isPrototypeOf(coder)));
- console.log("Coder.prototype.isPrototypeOf(coder): " + (Coder.prototype.isPrototypeOf(coder)));
- console.log("Function.prototype.isPrototypeOf(coder): " + (Function.prototype.isPrototypeOf(coder)));
- console.log("Object.prototype.isPrototypeOf(coder): " + (Object.prototype.isPrototypeOf(coder)));
- </script>
<script>function Employee(name){this.name = name;//this.getName = function(){return this.name};方法代码应该放到原型对象之中,而不是初始化函数中,这样每个employee对象都共享同一个方法代码}Employee.prototype.getName = function(){return this.name};var employee = new Employee('Jack');console.log("employee.getName(): " + employee.getName());//Jackvar employee2 = new Employee('Jack2');console.log("employee2.getName(): " + employee2.getName());//Jack2 function Coder(name,language){this.name = name;this.language = language;//this.getLanguage = function(){return this.language}; 方法代码应该放到原型对象之中,而不是初始化函数中,这样才能实现代码共享}Coder.prototype = new Employee('');Coder.prototype.constructor = Coder;//这一句话其实也可以不写,不影响继承Coder.prototype.getLanguage = function(){return this.language};var coder = new Coder('Coder Jack','Java');console.log("coder.getName(): " + coder.getName());//Coder Jackconsole.log("coder.getLanguage(): "+coder.getLanguage());//Javavar coder2 = new Coder('Coder Lee','C#');console.log("coder2.getName(): " + coder2.getName());//Coder Leeconsole.log("coder2.getLanguage(): " + coder2.getLanguage());//C#var coder3 = new Coder('Coder Liu','C++');console.log("coder3.getLanguage(): " + coder3.getName());//Coder Liuconsole.log("coder3.getLanguage()" + coder3.getLanguage());//C++console.log("employee.constructor: " + employee.constructor);console.log("employee.constructor.prototype === Employee.prototype: " + (employee.constructor.prototype === Employee.prototype));console.log("employee.constructor.prototype.constructor === Employee: " + (employee.constructor.prototype.constructor === Employee));console.log("employee instanceof Object: " + (employee instanceof Object));console.log("employee instanceof Function: " + (employee instanceof Function));console.log("employee instanceof Employee: " + (employee instanceof Employee ));console.log("Employee.prototype.isPrototypeOf(employee): " + (Employee.prototype.isPrototypeOf(employee)));console.log("Function.prototype.isPrototypeOf(employee): " + (Function.prototype.isPrototypeOf(employee)));console.log("Object.prototype.isPrototypeOf(employee): " + (Object.prototype.isPrototypeOf(employee)));console.log("coder.constructor: " + coder.constructor);console.log("coder instanceof Object: " + (coder instanceof Object));console.log("coder instanceof Function: " + (coder instanceof Function));console.log("coder instanceof Employee: " + (coder instanceof Employee ));console.log("coder instanceof Coder: " + (coder instanceof Coder ));console.log("Employee.prototype.isPrototypeOf(coder): " + (Employee.prototype.isPrototypeOf(coder)));console.log("Coder.prototype.isPrototypeOf(coder): " + (Coder.prototype.isPrototypeOf(coder)));console.log("Function.prototype.isPrototypeOf(coder): " + (Function.prototype.isPrototypeOf(coder)));console.log("Object.prototype.isPrototypeOf(coder): " + (Object.prototype.isPrototypeOf(coder)));</script>
- if (typeof Object.create !== 'function') {
- Object.create = function (o) {
- function F() {}
- F.prototype = o;
- return new F();
- };
- }
- var newObject = Object.create(oldObject);
- <!DOCTYPE HTML>
- <HTML>
- <HEAD>
- <TITLE>使用构造器模式(new + prototype)实现继承</TITLE>
- </HEAD>
- <BODY>
- <script>
- /**
- * 以原型对象为模板创建出新对象
- * 这个函数已经被Chrome和IE9采用,所以需要有个判断这个函数是否已经存在,Crockford的影响力可见一斑
- */
- if (!Object.create) {
- Object.create = function(oldObject) {
- function F() {
- }
- ;
- F.prototype = oldObject;
- return new F();
- }
- }
- /**
- * 在构造函数的原型对象上添加方法
- * 非常推荐这个函数,因为这个函数能够培养出在原型对象中定义方法的良好习惯
- */
- Function.prototype.method = function(name, func) {
- if (!this.prototype[name]) {
- this.prototype[name] = func;
- return this;
- }
- };
- /**
- * 使构造函数“继承”其他构造函数
- * 实际上是将构造函数的原型对象替换为另外构造函数产生的对象
- *
- */
- Function.method('inherits', function(F) {
- this.prototype = new F();
- return this;
- });
- /*****************************************
- *使用链式代码清晰紧凑地定义构造函数
- *****************************************/
- var Employee = function(name) {
- this.name = name;
- }.method('getName', function() {
- return this.name;
- });
- //由于method和inherits函数都返回this,所以可以非常舒服地将构造函数写成链式代码
- var employee = new Employee("jack");
- alert(employee.getName());
- //由于method和inherits函数都返回this,所以可以非常舒服地将构造函数写成链式代码
- var Coder = function(name, language) {
- this.name = name;
- this.language = language;
- }.inherits(Employee).method('getLanguage', function() {
- return this.language;
- }).method('getIntroduction', function() {
- return this.name + " is skilled in " + this.language;
- });
- var coder = new Coder('Jack', 'Java');
- alert(coder.getIntroduction());
- alert(coder.getName());
- </script>
- </BODY>
- </HTML>
- /*****************************************
- * 模拟私有变量
- *****************************************/
- var Employee = function(name) {
- //私有变量
- var name = name;
- this.getName = function() {
- return name
- };
- };
- var employee = new Employee('Jack');
- alert(employee.name);//undefined
- alert(employee.getName());//Jack
- /******************
- *模拟super.method()
- ******************/
- var Coder = function(name, language) {
- var _super = new Employee('');
- this.name = name;
- this.language = language;
- this.getName = function() {
- return "my name is :" + _super.getName.call(this, name);
- };
- }.inherits(Employee).method('getLanguage', function() {
- return this.language;
- }).method('getIntroduction', function() {
- return this.name + " is skilled in " + this.language;
- });
- var coder = new Coder('Jack', 'Java');
- alert(coder.getIntroduction());
- alert(coder.getName());//my name is Jack
- <!DOCTYPE HTML>
- <HTML>
- <HEAD>
- <TITLE>使用Object.create实现继承</TITLE>
- </HEAD>
- <BODY>
- <script>
- /**
- * 以原型对象为模板创建出新对象
- */
- if (!Object.create) {
- Object.create = function(oldObject) {
- function F() {
- }
- ;
- F.prototype = oldObject;
- return new F();
- }
- }
- /*****************************************
- * 使用原型对象创建对象,创建之后再对象初始化,
- * 这种创建方式直白地显示了原型语言创建对象的特点
- *****************************************/
- var employee = {
- name : 'Jack',
- getName : function() {
- return this.name;
- }
- };
- var coder = Object.create(employee);
- coder.name = 'Jackson';
- coder.language = 'Java';
- coder.getIntroduction = function() {
- return this.name + " is skilled in " + this.language;
- }
- alert(coder.getName());
- alert(coder.getIntroduction());
- </script>
- </BODY>
- </HTML>
- <!DOCTYPE HTML>
- <HTML>
- <HEAD>
- <TITLE>使用function实现继承</TITLE>
- </HEAD>
- <BODY>
- <script>
- /**
- * 以原型对象为模板创建出新对象
- * 这个函数已经被Chrome和IE9采用,所以需要有个判断这个函数是否已经存在,Crockford的影响力可见一斑
- */
- if (!Object.create) {
- Object.create = function(oldObject) {
- function F() {
- }
- ;
- F.prototype = oldObject;
- return new F();
- }
- }
- /**
- * 在构造函数的原型对象上添加方法
- * 非常推荐这个函数,因为这个函数能够培养出在原型对象中定义方法的良好习惯
- */
- Function.prototype.method = function(name, func) {
- if (!this.prototype[name]) {
- this.prototype[name] = func;
- return this;
- }
- };
- /**
- * 使构造函数“继承”其他构造函数
- * 实际上是将构造函数的原型对象替换为另外构造函数产生的对象
- *
- */
- Function.method('inherits', function(F) {
- this.prototype = new F();
- return this;
- });
- /*****************************************
- * 使用函数创建对象
- * 1 使用函数的闭包实现私有属性
- * 2 子对象可以调用父对象的方法
- *****************************************/
- function employee(name) {
- var object = {};
- //name属性是私有变量
- var name = name;
- var getName = function() {
- return name;
- }
- //getName对外公开
- object.getName = getName;
- return object;
- }
- function coder(name, language) {
- var object = employee(name);
- var superGetName = object.getName;
- var language = language;
- var getLanguage = function() {
- return language;
- };
- //调用父对象的方法
- var getName = function() {
- return "my name is " + superGetName();
- };
- object.getName = getName;
- return object;
- }
- var e1 = employee('Jack');
- alert(e1.name);//undefined
- alert(e1.getName());//Jack
- var c1 = coder('Jackson', 'Java');
- alert(c1.getName());//My name is Jackson
- </script>
- </BODY>
- </HTML>
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html
others
http://bonsaiden.github.com/JavaScript-Garden/zh/
- 2012-09-10 15:16
- 浏览 5427
- 评论(8)
- 论坛回复 / 浏览 (12 / 9604)
- 分类:Web前端
- 相关推荐
评论
this.name = name;
this.language = language;
}. method("getLanguage",function(){
return this.language;
}).inherits(Employee).method("getIntroduction",function(){
return this.name +" is a "+this.language + " coder."
});
var coder = new Coder('coder tom','java');
log("coder.name:"+coder.name);
log("coder.getName():"+coder.getName());
log("coder.language:"+coder.language);
log("coder.getLanguage():"+coder.getLanguage());
log("coder.getIntroduction():"+coder.getIntroduction());
function log(msg){
console.log(msg);
}
和你原帖中的区别在于红色标注的地方,inherits和method调换了下位置,在谷歌浏览器下运行时,log("coder.getLanguage():"+coder.getLanguage()); 这句提示coder.getLanguage())不是一个方法,把getIntroduction方法放在前面也是一样,求解释啊
this.name = name;
this.language = language;
}. method("getLanguage",function(){
return this.language;
}).inherits(Employee).method("getIntroduction",function(){
return this.name +" is a "+this.language + " coder."
});
var coder = new Coder('coder tom','java');
log("coder.name:"+coder.name);
log("coder.getName():"+coder.getName());
log("coder.language:"+coder.language);
log("coder.getLanguage():"+coder.getLanguage());
log("coder.getIntroduction():"+coder.getIntroduction());
function log(msg){
console.log(msg);
}
和你原帖中的区别在于红色标注的地方,inherits和method调换了下位置,在谷歌浏览器下运行时,log("coder.getLanguage():"+coder.getLanguage()); 这句提示coder.getLanguage())不是一个方法,把getIntroduction方法放在前面也是一样,求解释啊
- function newInstance(constructor) {
- var obj = {};
- // 这边的继承原型 有点欠缺;
- obj.__proto__ = constructor.prototype;
- constructor.apply(obj, Array.prototype.slice.call(arguments, 1));
- return obj;
- }
我在新的对象原型上添加方法的时候,必然会到时 old proto受到污染。
看错了 以为是继承
- function newInstance(constructor) {
- var obj = {};
- // 这边的继承原型 有点欠缺;
- obj.__proto__ = constructor.prototype;
- constructor.apply(obj, Array.prototype.slice.call(arguments, 1));
- return obj;
- }
我在新的对象原型上添加方法的时候,必然会到时 old proto受到污染。
Function本身是个函数,而所有的函数都继承Function.prototype。
可以用Function创建函数,而Function本身又是个函数,这是个鸡生蛋,蛋生鸡的问题
具体语言实现可能是这样:
native先构造好Function.prototype对象
然后Function对象和所有的函数指向Function.prototype
承蒙夸奖。您能看完,也很不容易,辛苦了。。。
这篇关于深刻理解JavaScript基于原型的面向对象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!
参考知识库