//创建Person构造函数,参数为name,age
function Person(name,age){
this.name = name;
this.age = age;
} function _new(){
//1.拿到传入的参数中的第一个参数,即构造函数名Func
var Func = [].shift.call(arguments);
//2.创建一个空对象obj,并让其继承Func.prototype
var obj = Object.create(Func.prototype);
//3.执行构造函数,并将this指向创建的空对象obj
Func.apply(obj,arguments)
//4.返回创建的对象obj
return obj
} xm = _new(Person,'xiaoming',18); console.log(xm);

来源https://blog.csdn.net/u010342862/article/details/80016695

if (typeof Object.create !== "function") {
Object.create = function (proto, propertiesObject) {
if (typeof proto !== 'object' && typeof proto !== 'function') {
throw new TypeError('Object prototype may only be an Object: ' + proto);
} else if (proto === null) {
throw new Error("This browser's implementation of Object.create is a shim and doesn't support 'null' as the first argument.");
} if (typeof propertiesObject != 'undefined') throw new Error("This browser's implementation of Object.create is a shim and doesn't support a second argument."); function F() {}
F.prototype = proto; return new F();
};
}

来源https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Polyfill

if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
} var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
// this instanceof fNOP === true时,说明返回的fBound被当做new的构造函数调用
return fToBind.apply(this instanceof fNOP
? this
: oThis,
// 获取调用时(fBound)的传参.bind 返回的函数入参往往是这么传递的
aArgs.concat(Array.prototype.slice.call(arguments)));
}; // 维护原型关系
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
// 下行的代码使fBound.prototype是fNOP的实例,因此
// 返回的fBound若作为new的构造函数,new生成的新对象作为this传入fBound,新对象的__proto__就是fNOP的实例
fBound.prototype = new fNOP(); return fBound;
};
}

来源https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

最新文章

  1. hadoop2.6.4 搭建单机模式
  2. linux head、tail、sed、cut、grep、find
  3. C# 将对象保存为文件 读取文件并转为对象 压缩文件 解压缩文件
  4. 用JAVA写一个日历计划
  5. TreeSet和TreeMap的输出
  6. 如何修改HDFS的备份数
  7. zabbix监控系统客户端安装
  8. Meta 整合
  9. 爬虫(heritrix框架)
  10. javase
  11. npm常用命令及版本号浅析
  12. Struts2-045验证脚本
  13. vue学习笔记(五)——指令
  14. eclipse打包
  15. 【52ABP实战教程】00-- ASP.NET CORE系列介绍
  16. 如何从二维数组中的多个key中获取指定key的值?
  17. Linxu命令与文件的搜索 - which, whereis, locate, find
  18. Elasticsearch Document
  19. 两层c:forEach循环嵌套
  20. Android-Java-抽象类

热门文章

  1. ffmpeg常用转换命令
  2. AI - 学习路径(Learning Path)
  3. 简介 - MongoDB
  4. dedecms中arclist标签做分页以及分页点击模块样式错乱问题
  5. c++中的左值与右值
  6. CentOS7.0小随笔——指令基本操作(Part.A)
  7. salesforce lightning零基础学习(十一) Aura框架下APP构造实现
  8. Talk In Web Security(安全世界观): Devleping a Secure WebSite
  9. Mysql加锁过程详解(4)-select for update/lock in share mode 对事务并发性影响
  10. Linux查询端口是否被占用的四种方法