在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内置的构造函数进行构造的。

javascript系列学习----Creating objects的更多相关文章

  1. Java中如何创建一个新的对象的/Creating Objects/

    The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't ...

  2. JavaScript - Standard built-in objects

    标准对象分类 Value Properties 以下全局属性返回一个简单的值:它们没有属性或者方法: Infinity NaN undefined null literal Function Prop ...

  3. [Javascript] Simplify Creating Immutable Data Trees With Immer

    Immer is a tiny library that makes it possible to work with immutable data in JavaScript in a much m ...

  4. [Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别

    条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...

  5. Creating objects on stack or heap

    class Player {  private: int health;  int strength;  int agility; public: void move(); void attackEn ...

  6. javascript系列学习----对象相关概念理解

    1.构造函数(相对于面向对象编程语言里面的类) 2.对象实例(它是由构造函数构造出来的对象,使用到关键字 new) 3.this关键字(往往是指我们的对象本身) 下面我们来看一个实例: var Per ...

  7. JavaScript Objects in Detail

    JavaScript’s core—most often used and most fundamental—data type is the Object data type. JavaScript ...

  8. JavaScript Garden

    Objects Object Usage and Properties Everything in JavaScript acts like an object, with the only two ...

  9. JavaScript- The Good Parts Chapter 3 Objects

    Upon a homely object Love can wink.—William Shakespeare, The Two Gentlemen of Verona The simple type ...

随机推荐

  1. Ansible 小手册系列 十五(Blocks 分组)

    当我们想在满足一个条件下,执行多个任务时,就需要分组了.而不再每个任务都要用when. tasks: - block: - command: echo 1 - shell: echo 2 - raw: ...

  2. LoadBalancerv2的原理分析

    OpenStack 是直接采用各种开源可用的负载均衡项目来完成负载均衡的任务,默认使用 HAProxy.LBaaSv2 本质来说,其实也是根据用户提出的负载均衡要求,生成符合的HAProxy配置文件并 ...

  3. python:input()和raw_input()

    1.input() 接受各种合法类型的输入,比如输入字符串,则需要使用双引号,否则报错. input()会自动判断类型,比如输入的是 1.1,则返回的类型是float. 示例: 2.raw_input ...

  4. laravel中上传图片之后图片的处理

    $file=Input::file('file'); if ($file->isValid()){ $entension=$file->getClientOriginalExtension ...

  5. 十九、dbms_resource_manager(用于维护资源计划,资源使用组和资源计划指令)

    1.概述 作用:用于维护资源计划,资源使用组和资源计划指令;包dbms_resource_manager_privs用于维护与资源管理相关的权限. 2.包的组成 1).dbms_resource_ma ...

  6. Python中的单例设计模式

    1)设计模式: 是前人工作的总结和提炼.通常,被人们广泛流传的设计模式.     某一问题的特定解决方案,使用设计模式是为了可重用代码,是代码更容易被人理解, 增加代码的可用性. 2)单例设计模式: ...

  7. windows7 下python3.6 下Scripts文件夹为空

    windows7 下python3.6 下Scripts文件夹为空,安装后不能运行pip,这个时候输入命令: python -m ensurepip 会自动安装pip,然后运行pip3 list就可以 ...

  8. Android 开发 Tip 17 -- 为什么getBackground().setAlpha(); 会影响别的控件?

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/75670018 http://www.jb51.net/article/110035.h ...

  9. Uncaught TypeError: jQuery.i18n.browserLang is not a function

    /********************************************************************* * Uncaught TypeError: jQuery. ...

  10. UNIX 基础知识

    登陆       1.登录名            系统在其 口令文件(通常是/etc/passwd文件) 中查看用户名,口令文件中包含了有关用户的信息.       2.shell          ...