此方法属于程序文档范畴,对接口的继承实现完全依靠程序员自觉
/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){ //implements People interface
this.name = name;
}
woman.prototype.showName = function(){
alert(this.name);
}
woman.prototype.createBody = function(){ //实现必要的方法
alert("身体已经创建好");
}
woman.prototype.createHead = function(){
alert("头部已经创建好");
}
把要实现的接口方法添加到类属性列表里,通过定义好的检测反复检查是否已经实现了那些方法
//优缺点:可以强迫程序员实现接口,没实现就报错。不过虽然声明了自己实现了哪些方法,但实现时很可能有遗漏
/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
woman.prototype.showName = function(){
alert(this.name);
}
woman.prototype.createBody = function(){ //实现必要的方法
alert("身体已经创建好");
}
woman.prototype.createHead = function(){
alert("头部已经创建好");
}
function implement(obj,interfaces){
for(var i=1;i<interfaces.length;i++){
var interfaceName = interfaces[i];
var interfaceFound = false;
for(var j=0;j<obj.implementsInterfaces.length;j++){
if(obj.implementsInterfaces[j] = interfaceName){
interfaceFound = true;
break;
}
}
if(!interfaceFound){
return false;
}
}
return true;
}
function isImplememts(instance,interfaces){ //判断对象是否已经继承相应接口
if(!implement(instance,interfaces)){
throw new Error("Object doesn't implement a required interface");
}
}
(不通过外表判断鸭子,而通过其是否有鸭子的特性来判断。如James Whitcomb Riley所说,像鸭子一样走路并且嘎嘎叫的就是鸭子)
上面俩种都声明了自己实现了那些接口,其实声明不重要,实现接口核心的是类实现了接口方法集。如果类具有了接口定义的所有方法函数名相同的函数,那么认为它实现了接口
//接口类,用来创建接口
var Interface = function(name,motheds){
if(agruments.length!=2){
throw new Error("Interface constructor called with "+arguments.length+"arguments,but expected exactly 2");
}
this.name = name;
this.methods = [];
for(var i=0;i<motheds.length;i++){
if(typeof motheds[i] !== 'string'){
throw new Error('Interface constructor expects mothed names to be'+'passes in as a string');
}
this.methods.push(motheds[i]);
}
}
Interface.prototype.ensureImplements = function(objs){
if(agruments.length != 1){
throw new Error("Interface constructor called with "+arguments.length+"arguments,but expected exactly 1")
}
for(var i=0;i<objs.length;i++){
var obj = objs[i];
for(var j=0;j<this.motheds.length;j++){
var mothed = this.methods[j];
if(!obj[mothed] || !typeof obj[mothed] !== 'function'){
throw new Error('Function Interface.ensureImplements:implements interface'+this.name+',obj.mothed'+mothed+'was not found');
}
}
}
}
//创建接口
var People = new Interface('People',['createHead','createBody']);
//子类
var Woman = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
Woman.prototype.showName = function(){
alert(this.name);
}
Woman.prototype.createBody = function(){ //实现必要的方法
alert("女人身体已经创建好");
}
Woman.prototype.createHead = function(){
alert("女人头部已经创建好");
}
//子类
var Man = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
Man.prototype.showName = function(){
alert(this.name);
}
Man.prototype.createBody = function(){ //实现必要的方法
alert("男人身体已经创建好");
}
Man.prototype.createHead = function(){
alert("男人头部已经创建好");
}
//判断是否实现
Poeple.ensureImplements(['Woman','Man']);
for循环示例;让用户输入行数,使用for循环嵌套打出倒着的星星出来,行数等于用户输入的数字 ;有1,2,3,4这么4个数,能组成多少个互不相同且不含有重复数字的三位数?都是多少?
&、|、~都是位操作符,而&&、|、~|都是逻辑操作!。&&是逻辑与运算符假前真后,||是逻辑或运算符真前假后,&是按位与操作两个数值的个位分别相与,同时为1才得1,只要一个为0就为0。
javascript中基本类型指的是那些保存在栈内存中的简单数据段,即这种值完全保存在内存中的一个位置。 引用类型指那些保存在堆内存中的对象,意思是变量中保存的实际上只是一个指针,这个指针指向内存中的另一个位置,该位置保存对象。
js实现栈或者队列有两种方式: 1.数组:数组本身提供栈方法(push,pop),队列方法(push,shift)。 2.链表:构造链表结构,说白了就是链表的插入(尾插),移除(栈:末尾节点移除,队列:头结点移除)
JavaScript怎么输出?输出方式有哪些?下面本篇文章就给大家介绍JavaScript的几种输出方式。window.alert()方法用于显示带有一条指定消息和一个【确认】 按钮的警告框。
当初有很多人说使用form方法将文件封装来上传,可是因为要照顾到从相机中选择图片,所以一直没有去做。 后来看到了Uploader的方法来传文件,感觉自己找到了 。是使用plus.uploader来完成的
document.write只能重绘整个页面,innerHTML可以重绘页面的一部分。 document.write是直接写入到页面的内容流,如果在写之前没有调用document.open, 浏览器会自动调用open。
Performance提供的方法可以灵活使用,获取到页面加载等标记的耗时情况。Performance.timing属性对象提供了浏览器从打开网页到加载完成之间各个节点的耗时数据,包括重定向开始、DNS查询、浏览器响应数据、DOM解析等相关节点
原理和 base64 是一样的,ASCII 共有94个可打印字符,base64 使用了其中 64 个,base91 使用了 91 个。
javascript中的location.href有很多种用法,window.location.href 语句可以实现一个框架的页面在执行服务器端代码后刷新另一个框架的页面
内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!