javaScript中对于对象之间的深层次继承.
//扩充 通过原型链prototype实现多層次對象继承
var ClassA = function(){
ClassA.prototype.name = 'jack';
ClassA.prototype.set_name = function(name){
this.name = name;
};
ClassA.prototype.get_name = function(){
return this.name;
};
}
var ClassB = function(){
ClassB.prototype = new ClassA();
ClassB.prototype.age = 25;
}
var ClassC = function(){
ClassC.prototype = new ClassB();
ClassC.prototype.color = 'red';
ClassC.prototype.get_message = function(){
return 'name :'+this.get_name()+"\n"+"age :"+this.age+"\n"+"color :"+this.color;
}
}
//ClassB()和ClassC()必須要運行 不可缺少 如果沒有執行這兩步操作則會報錯
//錯誤信息為 c.set_name is not a function TypeError
//對於這點我個人認為 ClassB和ClassC 是函數,需要編譯后才可以對它進行調用
//哪位兄台假如知道這裡的緣由,請留言或評論,謝謝
ClassB(); //必不可少
ClassC();//必不可少
var c = new ClassC();
c.set_name('michael');
console.log(c.get_message());
//結果為:
//name :michael
//age :25
//color :red
//通過for/in 對函數對象的屬性方法進行分離,并分別打印出來
function Traversal(obj){
var array1=[],array2=[],array3=[];
var array4=[];
for (var name in obj){
//對函數對象的屬性進行分析
if(typeof obj[name]== 'function'){
array3.unshift(name);
}else if(typeof obj[name] == 'function'){
array2.unshift(name);
}else if(typeof obj[name] !== 'function' || typeof obj[name] !=='object'){
array1.unshift(name);
}//保存所有的信息
array4.unshift(name);
};//返回一個多維數組
return new Array(array1,array2,array3,array4);
}
function test(){
var array = Traversal(new ClassC());
var myArray = new Array();
for(var i=0;i<array.length;i++){
myArray = array[i];
// console.log("this is array["+i+"] \n" +myArray +'\n 數組的長度為:' +myArray.length );
if(myArray.length>0){
console.log('----------------------------'+'\n'+"this is array["+i+"] \n" );
for(var j=0;j<myArray.length;j++){
console.log(myArray[j]+"\n");
}
}
}
}
test();
分享到:
相关推荐
在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Object.prototype.toString.call(arr)) //[object Array] 本文要讲的就是,...
为什么要用Object.prototype.toString而不是Function.prototype.toString或者其它?这是和他们的toString解释方式有关系的。下面是ECMA中对Object.prototype.toString的解释: 代码如下: Object.prototype.toString...
Object.prototype.hasOwnProperty.call快捷方式 安装 npm install --save has 用法 var has = require ( 'has' ) ; has ( { } , 'hasOwnProperty' ) ; // false has ( Object . prototype , 'hasOwnProperty' ) ; //...
【技术分享】从浅入深 Javascript 原型链与原型链污染 APT web安全 网络安全 安全 自动化
Object.prototype JavaScript是基于原型继承的,任何对象都有一个prototype属性。Object.prototype是所有对象的根,并且不可改变。 代码如下: Object.prototype=null; alert(Object.prototype);//[object ...
原型链继承 原理: 子类原型指向父类实例对象实现原型共享,即Son.prototype = new Father()。 这里先简单介绍下原型 js中每个对象都有一个__proto__属性,这个属性指向的就是该对象的原型。js中每个函数都有一个...
String.prototype.padStart 符合ES2017规范的String.prototype.padStart填充程序。 如果不可用,请调用其“ shim”方法对String.prototype.padStart进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作...
利用原型对象可以保存一些公共的属性和方法。当访问某个对象中的一个不存在的属性或方法时,会自动调用原型中的属性和方法。也就是说,基于原型创建的对象会自动拥有原型的属性和方法。 之前我们学过构造函数,通过...
array.prototype.at 符合ESnext规范的Array.prototype.at / polyfill / replacement可以使用到ES3。 该软件包实现了接口。 它在ES3支持的环境中工作,并符合建议的。 因为Array.prototype.at依赖于接收方( this...
json,jQuery,prototype
loops, and best practices on using types and data structures, as well as the coding style and recommended code organization patterns in JavaScript. The book will also teach you how to use arrays and ...
javascript prototype原型操作笔记.docx
原型链是一种机制,指的是JavaScript每个对象包括原型对象都有一个内置的[[proto]]属性指向创建它的函数对象的原型对象,即prototype属性。 作用:原型链的存在,主要是为了实现对象的继承。 一、 记住以下5句话...
建议使用Object.has()方法以使Object.prototype.hasOwnProperty()更易于访问。 Object.has()关于Object.has()方法的建议,以使Object.prototype.hasOwnProperty()更易于访问。 状态此提案尚未引入TC39。 ...
从“原始值和引用值类型及区别”到“EventLoop事件循环&宏任务和微任务 ”,整理了Javascript学习和面试中遇到的一些基础和常见的问题。 总共包含33个问答,部分问题带有代码解答。 1. 原始值和引用值类型及区别 2. ...
经典ajax.prototype.javascript实例
This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...
JavaScriptprototype的深度探索不是原型继承那么简单.pdf