前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JS构造函数优化 三步优化到位

JS构造函数优化 三步优化到位

作者头像
贵哥的编程之路
发布2020-10-28 15:51:33
7620
发布2020-10-28 15:51:33
举报
文章被收录于专栏:用户7873631的专栏
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        function mysay()
        {
             console.log("hello world");
        }
        function Person(myName,myAge)
        {
           // let obj=new Object();//系统做的事情
           // let this=obj;//系统做的事情
            this.name=myName;
            this.age=myAge;
            this.say=mysay;//用mysay不输出
        }
        let obj1 = new Person("lnj", 34);
        let obj2 = new Person("zs", 44);
         console.log(obj1.say === obj2.say);
         //这种方式解决了内存地址不一样的问题
    </script>
</body>
</html>
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
       let fns={
            mysay:function()
            {
                console.log("cyg");
            }
        };
        
        function Person(myName,myAge)
        {
           // let obj=new Object();//系统做的事情
           // let this=obj;//系统做的事情
            this.name=myName;
            this.age=myAge;
            this.say=fns.mysay;//用mysay不输出
        }
        let obj1 = new Person("lnj", 34);
        let obj1 = new Person("zs", 44);
        
        console.log(obj1.say === obj2.say);
         //这种方式解决了内存地址不一样的问题
         //用对象的方式解决了内存地址不一样的问题
         /* let fns = {
            test: function () {
                console.log("test");
            }
        }
        // 由于test函数都是属于同一个对象, 所以返回true
        //new重名的对象会错的因为没有什么都一模一样的对象
         console.log(fns.test === fns.test); // true
         */
    </script>
</body>
</html>
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
     // let fns = {
        //     mySay: function () {
        //         console.log("hello world");
        //     }
        // }
        function Person(myName, myAge) {
            // let obj = new Object();  // 系统自动添加的
            // let this = obj; // 系统自动添加的
            this.name = myName;
            this.age = myAge;
            // this.say = fns.mySay;
            // return this; // 系统自动添加的
        }
        Person.prototype={
            //类名.原型
            say:function()
            {
                console.log("hello world");
            }
        };
         let obj1 = new Person("lnj", 34);
        
        obj1.say();
        let obj2 = new Person("zs", 44);
        obj2.say();
        console.log(obj1.say === obj2.say);
        console.log(Person.prototype);//原型对象
        console.log(Person);//类
    </script>
</body>
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/08/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档