javascript深度克隆与javascript的继承实现
1、javascript深度克隆:
//注意这里的对象包括object和array
function cloneObject(obj){
var o = obj.constructor === Array ? [] : {};
for(var key in obj){
if(obj.hasOwnProperty(key)){
o[key] = typeof obj[key] === "object" ? cloneObject(obj[key]) : obj[key];
}
}
return o;
}
Object.prototype.cloneObject=function(){
var o=this.constructor===Array?[]:{};
for(var key in this){
if(this.hasOwnProperty(key)){
o[key] = typeof this[key] === "object" ? cloneObject(this[key]) : this[key];
}
}
return o;
}
//这个方法只能用于被拷贝的对象中元素中没有引用类型的值。
Object.prototype.cloneObject=function(){
var str=JSON.stringify(this);
return JSON.parse(str);
}
2、javascript的继承实现:
第一种 prototype 引用型原型继承
<script>
function parent(){
this.x=10;
}
function child(){
}
child.prototype=new parent();
var childObj=new child();
alert(childObj.x);
</script>
第二种 类继承 属性抄写
<script>
function parent(){
this.x=10;
}
function child(){
var parentObj=new parent();
for(var p in parentObj)this[p]=parentObj[p];
}
var childObj=new child();
alert(childObj.x);</script>
第三种 类继承 对象冒充
<script>
function parent(){
this.x=10;
}
function child(){
parent.call(this);
}
var childObj=new child();
alert(childObj.x);
</script>
第四种 原型抄写
<script>
function parent(){}
parent.prototype.me=function(){alert("parent")};
function child(){}
for(var p in parent.prototype){
child.prototype[p]=parent.prototype[p];
}
var childObj=new child();
childObj.me();
</script>
第五种 混合方式
混合了call方式、原型链方式
<script>
function Parent(hello){
this.hello = hello;
}
Parent.prototype.sayHello = function(){
alert(this.hello);
}
function Child(world){
Parent.call(this);//将父类的属性继承过来
this.world = world;//新增一些属性
}
Child.prototype = new Parent();//将父类的方法继承过来
Child.prototype.sayWorld = function(){//新增一些方法
alert(this.world);
}
var c = new Child("zhangsan","lisi");
c.sayHello();
c.sayWorld();
</script>
javascript深度克隆与javascript的继承实现的更多相关文章
- javascript深度克隆函数deepClone
javascript深度克隆函数deepClone function deepClone(obj) { var _toString = Object.prototype.toString; // nu ...
- Javascript深度克隆一个对象
Javascript中的对像赋值与Java中是一样的,都为引用传递.就是说,在把一个对像赋值给一个变量时,那么这个变量所指向的仍就是原来对 像的地址.那怎么来做呢?答案是“克隆”. 克隆有两种方法:一 ...
- 结构-行为-样式-Javascript 深度克隆函数(转)
突然想到有一回面试的时候有一个问题一直挂在心头,于是乎在网上找了找,这个比较好: //深度克隆 function deepClone(obj) { var result, oClass = isCla ...
- JavaScript深度克隆(递归)
今天在深度理解JQuery源码时,剖析extend时: jQuery.extend = jQuery.fn.extend = function() { //... } 感觉该方法的一部分功能与深度克隆 ...
- javascript 深度克隆对象
js一般有两种不同数据类型的值: 基本类型(包括undefined,Null,boolean,String,Number),按值传递: 引用类型(包括数组,对象),按址传递,引用类型在值传递的时候是内 ...
- JavaScript深度克隆
深度克隆函数: function deepClone(obj){ var str = ""; var newobj = obj.constructor === Array ? [] ...
- javascript深度克隆对象
/** * * @param obj * @returns {*} */ //深度克隆 function cloneObject(obj) { if (obj === null || typeof(o ...
- javascript 深度克隆
关键词 :递归 主要分为 数组 .对象.以及基本类型 function clone(Obj) { var buf; if (Obj instanceof Arr ...
- javascript中对象的深度克隆
记录一个常见的面试题,javascript中对象的深度克隆,转载自:http://www.2cto.com/kf/201409/332955.html 今天就聊一下一个常见的笔试.面试题,js中对象的 ...
随机推荐
- hive 配置mysql元数据库
在 hive的配置文件hive-site.xml中 <?xml version="1.0"?> <!-- Licensed to the Apache Softw ...
- 【译】 AWK教程指南 附录B-Actions
Actions 是由下列指令(statement)所组成: 表达式 ( 函数调用,赋值...) print 表达式列表 printf( 格式化字符串, 表达式列表) if( 表达式 ) 语句 [els ...
- iOS动画实现总结
在iOS中,动画实现方向有两种,一种是操作UIView的animation方法,另外一种就是核心动画,但到iOS7中,UIView又跟核心动画牵扯在一起. 方式一(利用核心动画添加动画) 核心动画的层 ...
- TFS的使用
1.http://www.kwstu.com/ArticleView/kwstu_201462311500744
- bzoj 1064【noi2008】假面舞会
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1064 给一个有向图染色,每个点的后继必须相同,问至少&至多有多少种染色方案 sol: ...
- ECSHOP在线手册之 数据库结构说明 (适用版本v2.7.3)
1.account_log 用户账目日志表 字段 类型 Null/默认 注释 log_id mediumint(8) 否 / 自增 ID 号 user_id mediumint(8) 否 / 用户登录 ...
- 【转】Netty那点事(一)概述
[原文https://github.com/code4craft/netty-learning/blob/master/posts/ch1-overview.md#%E5%90%88%E5%BC%80 ...
- Css基础-id选择器
id 选择器以#来定义 <p id="pid">Hello css</p> #pid { color:red; } <div id="div ...
- UITextView光标在中间的问题
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) { self.automatica ...
- oracle查找重复记录
SELECT *FROM t_info aWHERE ((SELECT COUNT(*) FROM t_info WHERE Title = a.Title) &g ...