zoukankan      html  css  js  c++  java
  • JavaScript函数与创建对象

    1.

    1. 通过new的方式创建对象,自己会返回一个对象

    function person(firstname, lastname, age, eyecolor) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.age = age;
        this.eyecolor = eyecolor;
        //     return [this.firstname,this.lastname,this.age,this.eyecolor,this] 
    }
    ​
    var myFather = new person("John","Doe",50,"blue");
    var myFather2 = new person("John","Doe",50,"blue");
    var myMother = person("Sally", "Rally", 48, "green");
    console.log(myFather)
    console.log(typeof myFather)
    console.log(myMother)
    console.log(typeof myMother)
    console.log("----------")
     
     
     
     
    
    
     

    2. 函数带有返回值过后, new与执行函数都会返回一个数组

     
    function person(firstname, lastname, age, eyecolor) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.age = age;
        this.eyecolor = eyecolor;
            return [this.firstname,this.lastname,this.age,this.eyecolor,this] 
    }
    ​
    var myFather = new person("John","Doe",50,"blue");
    var myFather2 = new person("John","Doe",50,"blue");
    var myMother = person("Sally", "Rally", 48, "green");
    console.log(myFather)
    console.log(typeof myFather)
    console.log(myMother)
    console.log(typeof myMother)
    console.log("----------")
     

     

    3. 在创建对象后,对象自己可以通过“实例名.xxx”添加,但是这个函数的其他对象无法得到新的属性。

    function person(firstname, lastname, age, eyecolor) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.age = age;
        this.eyecolor = eyecolor;
    //         return [this.firstname,this.lastname,this.age,this.eyecolor,this] 
    }
    ​
    var myFather = new person("John","Doe",50,"blue");
    var myFather2 = new person("John","Doe",50,"blue");
    var myMother = person("Sally", "Rally", 48, "green");
    console.log(myFather)
    console.log(typeof myFather)
    console.log(myMother)
    console.log(typeof myMother)
    console.log("----------")
    ​
    myFather.dd = ()=>{
        console.log("ddddddd")
    }
    ​
    myFather.name = "new name"
    ​
    person.prototype.ff = function() {
        return console.log("fffff");
    }
    ​
    console.log(myFather.name)
    myFather.dd()
    //myFather2.dd()//报错
    myFather.ff()
    //myMother.ff()//报错
  • 相关阅读:
    A New Approach to Line Simplification Based on Image Processing: A Case Study of Water Area Boundaries
    3D模型
    数码相机控制点的自动定位检校
    道路网匹配
    多线程操作数据拷贝要加线程锁
    编程琐事
    C++ 指定路径文件夹存在与否查询及文件夹创建
    C++ 网络编程之正确使用UDP广播及多播
    C++ 获得系统时间
    C++ 数据写入文件与读回
  • 原文地址:https://www.cnblogs.com/sunupo/p/15539271.html
Copyright © 2011-2022 走看看