• js高级知识点


    1.对构造函数的优化

            第一种:对构造函数优化的方法

    1. <script>
    2. function study(){
    3. console.log("我爱学习");
    4. }
    5. function Eat() {
    6. console.log("吃饭");
    7. }
    8. //第一种:对构造函数进行优化
    9. function Student(stuid, stuname) {
    10. this.stuid = stuid
    11. this.stuname = stuname
    12. this.study = study
    13. this.Eat = Eat
    14. }
    15. //实例化一个对象,使用new来实例化
    16. var stu_1 = new Student("1001", "张三")
    17. stu_1.study()
    18. stu_1.Eat()
    19. </script>

            第二种:对构造函数优化的方法

    1. <script>
    2. var fns = {
    3. study: function () {
    4. console.log("我爱学习");
    5. },
    6. Eat: function () {
    7. console.log("吃饭");
    8. }
    9. }
    10. //第二种:对构造函数进行优化
    11. function Student(stuid, stuname) {
    12. this.stuid = stuid
    13. this.stuname = stuname
    14. this.study = fns.study
    15. this.Eat = fns.Eat
    16. }
    17. //实例化一个对象,使用new来实例化
    18. var stu_1 = new Student("1001", "张三")
    19. stu_1.study()
    20. stu_1.Eat()
    21. </script>

    2.原型prototype

            使用原型时,一般情况下,对象的属性不会放在原型上创建,会直接放在构造函数中,除非这个属性的值是共享的数据,此时就可以放在原型上;一般需要共享的函数也可以放在原型上,这样的话,每个实例化的对象中都包含了此函数 

                    构造函数、原型对象和实例化对象三者之间的关系

                    构造函数创建时会自动添加一个prototype原型对象,prototype又可以通过constructor来重新指向该构造函数

                    构造函数可以通过new实例化一个对象,此时会添加一个隐式原型_proto_,那么该对象就可以通过对象._proto_.constructor重新指向该构造函数

                  实例化对象可以通过隐式原型__proto_指向prototype原型对象

    1. console.log(Student.prototype);
    2. Student.prototype.age = 20 //给原型prototype中创建一个属性age
    3. Student.prototype.running = function () {//给原型prototype中创建一个属性running
    4. console.log("跑步");
    5. }
    6. console.log(stu_1.age);
    7. console.log(Student.prototype);
    8. stu_1.running()
    9. console.log(stu_1.age);
    10. console.log(Student.prototype);
    11. // 通过Student的原型对象中的constructor,可以重新指向Student
    12. console.log(Student.prototype.constructor === Student);
    13. //通过实例化对象时会创建一个隐式原型__proto__,它指向的就是Student.prototype
    14. console.log(stu_1.__proto__ === Student.prototype);
    15. //通过实例化对象的隐式原型__proto__中的constructor就可以指向Student
    16. console.log(stu_1.__proto__.constructor === Student);

    3.原型链

            访问一个对象的属性时

            先在自身属性中查找,找到返回

            如果没有,再沿着_proto_这条链上查找,找到返回

            如果最终没有找到,返回undefined

            别名:隐式原型链

            作用:查找对象的属性(方法)

                    stu_1.age=30

                    console.log(stu_1.age);

                    hasOwnProperty:检查对象本身是否包含某个属性,

                    如果有则返回true,反之则返回false

                    console.log(stu_1.hasOwnProperty("age")); //false

                    console.log(stu_1.__proto__.hasOwnProperty("age"));  //true

                    in:检查对象中是否包含某个属性,如果对象本身没有,

                    但原型对象上有,也可以返回true

                    console.log("age" in stu_1);   //true */

    1. // 练习 原型链
    2. //现由一个构造函数Student
    3. function Person(name,sex){
    4. this.name = name
    5. this.sex = sex
    6. }
    7. var p0 =new Person("zhangsan","男")
    8. // p0.age = 20
    9. // Person.prototype.age = 30
    10. console.log(p0.age); //打印出20,打印出30,打印出undefined
    11. console.log(p0.__proto__ === Person.prototype); //打印为true
    12. console.log(p0.__proto__.constructor == Person); //打印为true
    13. console.log(Person.prototype.constructor ==Person); //打印为true
    14. console.log(Person.prototype.__proto__ === Object.prototype); //打印为true
    15. console.log(Object.prototype.__proto__); //打印为null

  • 相关阅读:
    ROS学习笔记(6):ros_control
    java-php-python-航空订票管理系统计算机毕业设计
    java计算机毕业设计小区广告位招商系统源码+系统+数据库+lw文档+mybatis+运行部署
    python中的异常处理try-except-else-finally
    NameNode的image和edits里面到底存放了些啥
    做项目管理需要哪些技能?
    代理IP和Socks5代理:跨界电商与爬虫的智能引擎
    哈希表的前置知识---二叉搜索树
    通达OA 2016网络智能办公系统 handle.php SQL注入漏洞
    【快应用】deeplink 3种跳转格式现象总结
  • 原文地址:https://blog.csdn.net/m0_62073591/article/details/126589243