js内置函数的改写问题

点这里先看看什么是js内置函数?

简单点说内置函数就是浏览器内核自带的函数,访问他的constructor属性时可以看到‘[ native code ]’ 字样的函数,举个例子在控制台打出:

alert.constructor
ƒ Function() { [native code] }

那么是不是只要函数的constructor属性包含native code字符串就表示函数没有被篡改过呢?
不是的,改写过后函数的constructor里面可以包含特征字符串
举个栗子:

console.log.constructor
ƒ Function() { [native code] }
Function.prototype.toString = function (){return 'function' + this.name + '() {       [native code] }'}
ƒ () {       [native code] }
console.log.constructor
ƒ Function() {       [native code] }
console.log('miao')
miao
console.log = function(){alert('miao')}
ƒ () {       [native code] }
console.log('jiujiu')
//页面上弹出框并显示‘miao’

常规的改写方式

var _alert = alert;
alert = function(a){
console.log('来了一次');
return _alert(a)
}

本身没有prototype的函数在改写之后就会有prototype属性,所以可以通过访问内置函数的prototype属性来判断函数是否被改写

alert.prototype 
undefined
var a = alert;
alert = function(y){
console.log('hdfjk');
return a(y);
};
alert.prototype 
{constructor: ƒ}

总结

要看(本身没有prototype的)函数是否被改写,可以通过查看其是否有prototyp;要看(本身有prototype的)函数是否被改写,可以直接将函数打出来比对正常值

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,430评论 0 3
  • 函数和对象 1、函数 1.1 函数概述 函数对于任何一门语言来说都是核心的概念。通过函数可以封装任意多条语句,而且...
    道无虚阅读 4,692评论 0 5
  •   面向对象(Object-Oriented,OO)的语言有一个标志,那就是它们都有类的概念,而通过类可以创建任意...
    霜天晓阅读 2,172评论 0 6
  • 董小姐 你从没忘记你的微笑 就算你和我一样 渴望着衰老 董小姐 你嘴角向下的时候很美 就像安和桥下 清澈的水 董小...
    mannoLYX阅读 355评论 0 0
  • 鸟焚其巢 旅人先笑后号啕
    九迁阅读 137评论 0 0