Js new的本质
2016-03-11 14:55
435 查看
new的本质
如果new运算符是一个方法而不是一个运算符.
Function.prototype.new = function(){
// 创建一个新对象that,新对象继承自构造函数
var that = Object.create(this.prototype);
// 用新函数的指针that,调用构造函数,并得到返回值other
var other = this.apply(that,arguments);
// 判断构造函数中,return的是对象,是则返回,不是则返回新对象that
return (typeof other === ‘object’ && other || that);
}
var child = Parent.new(arguments); –> child = new Parent(arguments);
如果new运算符是一个方法而不是一个运算符.
Function.prototype.new = function(){
// 创建一个新对象that,新对象继承自构造函数
var that = Object.create(this.prototype);
// 用新函数的指针that,调用构造函数,并得到返回值other
var other = this.apply(that,arguments);
// 判断构造函数中,return的是对象,是则返回,不是则返回新对象that
return (typeof other === ‘object’ && other || that);
}
var child = Parent.new(arguments); –> child = new Parent(arguments);
相关文章推荐
- JS dom对象
- JS window.open()属性
- 生成jsp验证码的代码详解(servlet版)
- Jsp页面报错状态码含义
- js--3目运算符
- js window的使用
- js 执行环境及作用域
- js 原型模型重写1
- javascript Date format(js日期格式化)
- javascript统计输入文本的简易方法
- Javascript进阶篇——(DOM—认识DOM、ByName、ByTagName)—笔记整理
- 收集Web前端 面试题 Javascript部分
- JS正则表达式验证账号、手机号、电话和邮箱
- OC与JavaScript交互学习过程中的资料整理
- Json FastJson Gson介绍与使用
- JS实现DIV盒子高度为宽度2倍
- jsoup爬取指定网页的url和图片
- JS验证电话和传真号码格式
- JS验证手机号码格式
- JS验证用户名必须以字母(不区分大小写)、数字、下划线(_)随意组合的字符