热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javascript继续

javascript是一门比较难通晓的言语,原因是其他言语供应机制,javascript供应要领。在我看来,言语如java,python比较勤学的言语有一个特性:关键字多。java

Javascript是一门比较难通晓的言语,原因是其他言语供应机制,Javascript供应要领。
在我看来,言语如java,python比较勤学的言语有一个特性:关键字多。java供应extends,implements,以及特定的包治理机制。python特别的文本构造,大批API等。这些言语能够使初学者疾速上手,由于大多数需求的满足都是靠机制。言语供应的关键字,库治理,API。一句话就是人人写的代码都一样,思绪一样,代码就一样(或差别不大,由于设想形式的存在,有构造差别,但底层构造也雷同)。
另一种言语供应作用域,内存规划,言语自身的默认设置等。这类言语如c,c++,Javascript,函数式言语。自身是带有一种编译器或诠释器的觉得。就是用户身在其中,须要晓得一些编译器开辟人员才须要的学问。就以Javascript来说吧,要想完成继续,你就得相识诠释器在读取一个对象的属性的过程当中是怎样寻觅的(原型链)。c言语:宏,你得相识预处理器;全局变量,连接器;C++:struct和class的区分,vptr,多继续等。
说这么多,只是想为本身花了半年学Javascript的痛楚吐槽一下,并没有比较言语的意义,莫喷!java那种思绪一下,代码差别不大的上风在企业级开辟中才表现,so i love java!绝没有瞧不起的意义。

继续是什么

以java为例:

class Parent{

String name; //private field
Parent(String name){
this.name = name;
}

}

class Child{

int id; //private field
Child(String name,int id){
super(name);
this.id = id;
}

}

继续的重点:

1.Child实例化之前,Parent必需实例化
2.Child对象能够运用Parent对象的要领和属性
3.Child能够增加本身的要领,假如要领名雷同,则掩盖Parent的要领
4.Child能够增加本身的静态要领,能够继续Parent的父类要领

简朴继续

这也是网上比较多的继续要领(类式继续):

function Parent () {
this.ParentValue = true; //private field
}
Parent.staticP = 1;
Parent.prototype.getValue = function () {
return this.ParentValue;
}
function Child () {
this.ChildValue = true; //private field
}
Child.prototype = new Parent();
Child.prototype.getValue = function () {
return this.ChildValue;
}
Child.staticC = 1;

这类体式格局不能满足上面的4个前提:
1.满足,然则否是完全雷同。Parent在Child定义时就已实例化了,Parent实例化太超前了,致使一切Child对象共享一个Parent实例,假如一个Child对象转变了Parentvalue,一切Child对象都发作转变。
2.满足
3.满足
4.不满足
所以说基础满足2.5条吧

满足前提的继续

function Parent () {
this.ParentValue = true; //private field
}
Parent.staticP = 1;
Parent.prototype.getValue = function () {
return this.ParentValue;
}
function Child (p,c) {
Parent.call(this,p);
this.ChildValue = c; //private field
}
Child.staticC = 1;
inheritPrototype(Parent,Child);
Child.prototype.getValue = function () {
return this.ChildValue;
}
function inheritPrototype (Parent,Child) {
merge(Parent,Child); //将Parent上的属性安排到Parent上
var p = inheritObject(Parent.prototype);
p.cOnstructor= Child;
return p;
}
function inheritObject (o) {
function F(){}
F.prototype = o;
return new F();
}
//将Parent上的属性安排到Parent上
function merge (dist,source) {
for(attr in source){
if(source.hasOwnProperty(attr)){
if(!dist.hasOwnProperty(attr)){
dist[attr] = source[attr];
}
}
}
}

更好的写法

我参考Backbone的完成
Backbone依靠Underscore

function Parent () {
this.ParentValue = true; //private field
}
Parent.staticP = 1;
function Child (p,c) {
Parent.call(this,p);
this.ChildValue = c; //private field
}
function extend(Parent,Child,protoProps,staticProps){
_.extend(Child, Parent, staticProps);
Child.prototype = _.create(Parent.prototype, protoProps);
Child.prototype.cOnstructor= Child;
return Child;
}
extend(Parent,Child,{
getValue: function() {
return this.ParentValue;
}
}, {
staticC: 1
});
是否是更轻易呢!

推荐阅读
author-avatar
pz51747pz你
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有