爬虫逆向补环境(3)-原型与原型链前言在浏览器里面可以把原型看成浏览器内置对象的创建类原型链指的是创建的类和它的父类之间的关系浏览器内置对象拥有固定原生原型链结构反爬会检测对象之间的原型引用链条原型上的属性对比标准浏览器特征爬虫工具往往会篡改原型链重写原生原型方法一旦校验不匹配就会被识别为爬虫学习原型与原型链之前我们先了解一下javascript中类的创建与继承类使用class关键字创建使用constructor完成参数初始化类似于python中的__init__构造函数extends完成继承classPerson{constructor(name){this.namename;}sayHello(){console.log(${this.name}say hello);}}classStudentextendsPerson{constructor(name,age){super(name);this.ageage;}introduce(){console.log(${this.name}is${this.age}years old);}}varstudent_1newStudent(Tommy,18);console.log(student_1);student_1.sayHello();student_1.introduce();上面的子类实例里面没有introduce方法但是成功调用了然后我们引入了/原型/的概念原型本身是一个普通的对象用来给别的对象共享属性和方法的javascript里面类创建的方法默认被传入到了原型里面所以我们可以直接在子类实例对象里面直接调用到introduce方法。console.log(student_1.hasOwnProperty(introduce));console.log(student_1.__proto__.hasOwnProperty(introduce));console.log(student_1.__proto__.__proto__.hasOwnProperty(introduce));console.log(Student.prototype.hasOwnProperty(introduce));这是基于上面代码的拓展里面_proto__指的是隐式原型prototype指的就是原型student_1._protoStudent.prototype即类和其创建的类实例对象是全等的写两个__proto__就是指该类的父类的隐式原型hasOwnproperty:检测一个属性是否为自身所拥有有就返回true,没有就返回false根据上面的输出可以说明创建的introduce方法被自动上传在了Student的原型里面,所以我们可以在实例对象student_1里面直接调用示例1这里说明navigator的userAgent属性不在navigator对象下面而是在原型上面//写一个脚本functionNavigator(){}Navigator.prototype.userAgentxxxnavigatornewNavigator()我们应该这样补环境示例2这里说明该属性写在了document的父类的原型里面dir(document)查看得到document在HTMLDocument里面HTMLDocument在Document里面functionDocument(){}Document.prototype.createElementfunction(tag_name){console.log(tag_name);}functionHTMLDocument(){}// 类之间的原型关系绑定第一个是子类第二个是父类Object.setPrototypeOf(HTMLDocument.prototype,Document.prototype);documentnewHTMLDocument();console.log(document.hasOwnProperty(createElement));console.log(document.__proto__.hasOwnProperty(createElement));console.log(document.__proto__.__proto__.hasOwnProperty(createElement));原型之间的关系就要这样写补充document里面createElement属性经常检测toString这个属性必须和浏览器保持一致Document.prototype.createElement.toStringfunction(){returnfunction createElement() { [native code] };}
阅读完成 · 觉得有帮助?