JS中的继承和Extend

来源:互联网 发布:怎么在淘宝上买小号 编辑:程序博客网 时间:2024/06/02 15:13

Extend

// 例子:浅拷贝var a = {};var b = {c:1, d:2};for(var i in b){  a[i] = b[i];}// 封装,对引用类型(数组、对象)没有用function extend(sub,sup){  for(var i in sup){    sub[i] = sup[i];  }}

继承

var People = function (){  this.name = 'rzy';}People.prototype.getName = function(){  return this.name;}var Man = function (){  this.sex = 'male';  People.call(this);}/* 土鳖方法,加入后期People中的name改变了,那Man的值也会改变Man.prototype = People.prototype;var man = new Man();man.getName(); // rzy*/Man.prototype = new People();Man.prototype.constructor = Man;var man = new Man();man.getName();  // rzy


0 0