构造函数的this是指向实例对象
作者:互联网
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head>
<body> <div></div> <script> function Star() { this.name = '我是原型' this.a = function() { console.log(this.name); console.log(this); } } Star.prototype.sing = function() { console.log('哈哈哈11'); console.log(this); };
var sss = new Star(); sss.name = '我是创建的'; sss.try = function() { console.log(this.name); } sss.a(); </script> </body>
</html>
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head>
<body> <div></div> <script> function Star() { this.name = '我是原型' this.a = function() { console.log(this.name); console.log(this); } } Star.prototype.sing = function() { console.log('哈哈哈11'); console.log(this); };
var sss = new Star(); sss.name = '我是创建的'; sss.try = function() { console.log(this.name); } sss.a(); </script> </body>
</html>
标签:function,console,log,指向,sss,name,实例,Star,构造函数 来源: https://www.cnblogs.com/zhengjiajian/p/15673550.html