Javascript - Prototype Based Language

Javascript - Prototype Based Language

作者:heroboy

  最近看了一下firefox中javascript的实现,虽然源代码看不大懂,但对javascript本身有了进一步的认识。javascript是一门通用的脚本语言,它并不仅仅用在web上,而是作为嵌入的脚本语言使用的。不过我看到js除了在web上并没有很广泛的应用(和lua,python等比较的话),不知道为什么。

Prototype Based VS Class Based

  javascript和c++/Java等一样,是面向对象的语言,不同之处在于c++/Java是class based OO language,而javascript是prototype based OO language。
  在class based OO language中严格区分类和实例(或者说对象);而javascript中只有对象(这点和python也是不同的,在python中Class虽然是一个对象,但是python还是有类Class这个概念的,然后通过这个类创建实例的)。

Runtime Member

  在C++/Java中,由于类是静态的(再编译的时候决定的),所以一个对象有哪些成员函数、成员变量是固定的,不能在运行的时候改变。而在Javascript中,一个对象可以在运行时添加和删除它的成员,你可以这么写:
var Mary = new Object;
Mary.age=10;
Mary.eat=function (food) {/*吃东西*/};
其实Javascript的成员有点类似于Lua 中的成员或者c++中的关联容器,例如下面的c++代码:
void eat(void * food)
{
	/*吃东西*/
}
map<string,void*> Mary;
Mary["age"]=(void*)new int(10);
Mary["eat"]=(void*)eat;
当然上面的代码纯粹乱写,因为c++的变量有类型的,所以只能用void*来代替。
p.s. 实际上在Javascript中,你可以这么访问成员:
Mary["age"]=10; 
var str="write";
document[str]("hello");

Function As Constructor

在Javascript中刚刚创建的Object是没有任何成员的,可以使用一个构造函数来初始化新建的对象。
function Person(name,age)
{
	this.name=name;
	this.age=age;
	this.eat=function (food)//成员函数其实不应该这么写,下面会说明的.
	{
		document.write(this.name+"吃了"+food+"<br>");
	}
}
var Mary=new Person("Mary",10); 
var Tom =new Person("Tom",11); 
Mary.eat("apple");
new操作作了2件事:
1.新建一个Object 。
2.把这个Object作为this调用构造函数(new后面的参数)。

Function Is Also Object

  在Javascript中函数也是一个Object,只不过是一个内建的Object,只不过它的构造函数Javascript本身已经提供,而且可以用特殊的语法来创建Function Object。
function add(x,y)
{
	return(x + y);//Perform addition and return results.
}
var add = new Function("x", "y", "return(x+y)");

  上面两种语法创建add都是一样的,同样你一样可以写add.age=10;,类似于Function还有Array,String,Number等内建的对象。这些内建的对象都有一些特殊的成员,使得它们有的能相加(String,Number,Array等),有的能调用(Function),等等。当然Javascript本身不能重载运算符的。
  现在可以知道上面Person函数中有什么问题了吧!每次新建一个Person的时候都会新建一个Function Object,而eat方法应该是所有Person都共享的,这就要用过prototype来解决。

What''s Prototype

  任何对象都有一个Prototype,Prototype是另一个对象。当访问一个对象的成员的时候,如果这个对象不存在这个成员,那么Javascript会到它的Prototype对象上去找,找不到再到Prototype的Prototype(Prototype也是一个对象所以它也有Prototype)上去找,如此重复,直到不存在Prototype为止(这是特例,你是写不出代码让你的对象的Prototype不存在的)。
  那么如何设定一个对象的Prototype呢?每个Function Object都有一个prototype属性,不过这不是这个函数的Prototype,而是当这个函数作为构造函数的时候,它把prototype属性作为新建对象的Prototype。(请注意上面我写的prototype和Prototype的区别,包括下面要写的)
  所以,只要给Person这个函数的prototype属性增加eat方法,就可以在Mary和Tom中共享这个eat了(可以测试一下Mary.eat==Tom.eat)。上面的代码只要改成:

function Person(name,age)
{
	this.name=name;
	this.age=age;
}
var Mary=new Person("Mary",10);//创建Mary
Person.prototype.eat=function (food)//修改Prototype
			{
			document.write(this.name+"吃了"+food+"<br>");
			}
var Tom =new Person("Tom",11);//创建Tom
Tom.eat("banana");
Mary.eat("apple");//即使Mary先创建,eat方法是后添加的。
或者:
var ClassPerson = new Object;
ClassPerson.eat=function (food){document.write(this.name+"吃了"+food+"<br>");}
function Person(name,age)
{
	this.name=name;
	this.age=age;
}
var Mary=new Person("Mary",10);//创建Mary,Mary的Prototype是默认的(默认的是Object)
Person.prototype=ClassPerson;//改变Prototype
var Tom =new Person("Tom",11);//创建Tom,Tom的Prototype才是ClassPerson.
Tom.eat("banana");
//Mary.eat("apple");//不可以,这时Mary的Prototype和Tom的Prototype不是同一个了。
通过Prototype可以在对象之间共享方法和属性,通过设置一层一层的Prototype可以实现继承,不过不能多重继承。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Initialization error (ESLint). Cannot find module 'C:/Users/14304/Desktop/JAVA/JinRongKanBan/pc_前端源码/node_modules/eslint/lib/options' Require stack: - C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-common.js - C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-plugin.js - C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-plugin-provider.js - C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\jsLanguageServicesImpl\js-language-service.js Error: Cannot find module 'C:/Users/14304/Desktop/JAVA/JinRongKanBan/pc_前端源码/node_modules/eslint/lib/options' Require stack: - C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-common.js - C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-plugin.js - C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-plugin-provider.js - C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\jsLanguageServicesImpl\js-language-service.js at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15) at Module._load (node:internal/modules/cjs/loader:922:27) at Module.require (node:internal/modules/cjs/loader:1143:19) at require (node:internal/modules/cjs/helpers:110:18) at requireInContext (C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-common.js:24:12) at new ESLintPlugin (C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-plugin.js:33:61) at ESLintPluginFactory.create (C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\languageService\eslint\bin\eslint-plugin-provider.js:25:34) at Interface.<anonymous> (C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\plugins\javascript-impl\jsLanguageServicesImpl\js-language-service.js:82:52) at Interface.emit (node:events:513:28) at [_onLine] [as _onLine] (node:internal/readline/interface:422:12) Process finished with exit code -1
07-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值