zoukankan      html  css  js  c++  java
  • javascript系列学习----Creating objects

    在javascript语言里面,一切皆是对象,对象是它的灵魂,值得我们学习和领悟对象的概念和使用,下面我会引用实例来进行说明。

    1)创建对象

    方法一:js的对象方法构造

    var cody = new Object();   //produces an Object() object

    cody.living = true;

    cody.age = 33;

    cody.gender = 'male';

    cody.getGender = function(){return cody.gender;};

    console.log(cody.getGender()); // 方法,logs 'male'

    console.log(cody.age);  //属性

    console.log(cody);    //输出整个对象

    方法二,自己写构造函数完成对象创建

    var Person = function(living, age, gender) {

        this.living = living;

        this.age = age;

        this.gender = gender;

        this.getGender = function() {return this.gender;};
    };

    // instantiate a Person object and store it in the cody variable

    var cody = new Person(true, 33, 'male');

    console.log(cody);

    方法三,数组创建

    // instantiate an Array object named myArray

    var myArray = new Array(); // myArray is an instance of Array

    // myArray is an object and an instance of Array() constructor

    console.log(typeof myArray); // logs object! What? Yes, arrays are type of object

    console.log(myArray); // logs [ ]

    console.log(myArray.constructor); // logs Array()

    其它方法,javascript 语言就是一门非常灵活的web开发语言,它没有严格的类型。它的灵活性很大就体现在它的对象设计思想上面,下面是九种预置的对象构造函数。我们也可以使用它们来创建我们想要的对象。

    ✴ Number()
    ✴ String()
    ✴ Boolean()
    ✴ Object()
    ✴ Array()
    ✴ Function()
    ✴ Date()
    ✴ RegExp()
    ✴ Error()

    总结一下,javascript创建对象的方法就分两类,一种是我们自己构造的对象(如方法二);另一种就是利用javascript内置的构造函数进行构造的。

    本博客的所有博文,大都来自自己的工作实践。希望对大家有用,欢迎大家交流和学习。 我的新站:www.huishougo.com
  • 相关阅读:
    node js的模块
    前端学习ES6
    产品相关
    linux命令
    【jmeter】对于返回结果中文显示?问题
    MAC 本用pip3命令安装openpyxl插件(或者其他插件)后,在Pycharm依然找不到
    pip3版本已经是最新,安装openxl失败,Could not find a version that satisfies the requirement openxl
    MAC本安装python3.8后,pip3命令无法更新问题
    mac安装python环境
    nodejs安装步骤
  • 原文地址:https://www.cnblogs.com/zhouqingda/p/5424824.html
Copyright © 2011-2022 走看看