• js中创建对象的5种方法


    1、原始模式

    var  dog  =    {
        name: jack,
        length: 70,
        wang:function(){
            console.log(this.name);
        }

    2、工厂模式(批量)

    function  creatdog  (name,length) {
        var  moudle  =   new  object ();
        dog.name=name;
        dog.length=length;
        dog.wang=function(){
            console.log(this.name);
        };
        return moudle;
    }

    3、构造函数

    function  Dog (name,length) {
        this.name=name;
        this.length=length;
        this.wang=function() {
            console.log(this.name);
        };
    }
    var dog1 = new Dog( 'wangcai' , 80) //创建一个dog1对象
    
    Dog('wangcai', 80);    //属性方法都给window对象,window.name='wangcai',window.wang()会输出wangcai

    4、原型模式

    function  Dog () {}
    Dog.prototype.name = 'wangcai';
    Dog.prototype.length=80;
    Dog.prototype.wang= function () {
        console.log(this.name);
    };
    
    var  dog1 = new Dog();
    // dog1:name:'wangcai';length:80;f:wang()

    5、混合模式

    function Dog (name, length) {
     
        this.name = name;
     
        this.length = length;
     
    }
     
    Dog.prototype = {
     
        hobby: ['running','eat'];
     
        wang: function () { 
        console.log(this.name);
       } };
    var dog1 = new Dog('wangcai', 80); //dog1:'wangcai',80; __proto__: ['running','eat'],wang var dog2 = new Dog('defu', );70 //dog2:'defu',70;__proto__: ['running','eat'],wang
  • 相关阅读:
    WEB-INF下的jsp通过servlet中超链接跳转
    WEB-INF下的jsp怎么访问
    迭代器一般用法
    接口深层理解
    java中的接口深层理解
    动态SQL与静态SQL的区别
    TIDB集群部署
    ora-00245报错解决方法
    PostgreSQL 密码验证功能增强
    多台机器之间一键化互信脚本实现
  • 原文地址:https://www.cnblogs.com/jswzy/p/10165842.html
Copyright © 2020-2023  润新知