在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. 到底啥是平台,到底啥是中台?李鬼太多,不得不说(ZT)

    (1)哪些不是中台,而是应该叫平台 做开发,有所谓的三层技术架构:前端展示层.中间逻辑层.后端数据层.我们现在讲的中台不在这个维度上. 做开发,还有所谓的技术中间件.一开始我们没有中间件的概念,只有操 ...

  2. Ansible 小手册系列 三(命令介绍)

    仅仅只是介绍,可以选择跳过 ansible ansible是指令核心部分,其主要用于执行ad-hoc命令,即单条命令.默认后面需要跟主机和选项部分,默认不指定模块时,使用的是command模块. Us ...

  3. vue踩坑之旅 -- computed watch

    vue踩坑之旅 -- computed watch 经常在使用vue初始化组件时,会报一些莫名其妙的错误,或者,数据明明有数据,确还是拿不到,这是多么痛苦而又令人忍不住抓耳挠腮,捶胸顿足啊 技术点 v ...

  4. 018——VUE中v-for操作对象与数值

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制

    Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...

  6. 多线程相关(pthread 、NSThread 、GCD、NSOperation)

    进程 进程是指在系统中正在运行的一个应用程序 线程 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) 1个线程中任务的执行是串行的(执行完上一个才能执行下一个) 多线程 1个进程中可以 ...

  7. New Concept English three(10)

    The great ship, Titanic, sailed for New York from Southampton on April 10th, 1912. She was carrying ...

  8. Vim技能修炼教程(15) - 时间和日期相关函数

    Vimscript武器库 前面我们走马观花地将Vimscript的大致语法过了一遍.下面我们开始深入看一下Vimscript都给我们准备了哪些武器.如果只用这些武器就够了,那么就太好了,只用Vimsc ...

  9. Deep Reinforcement Learning 基础知识(DQN方面)

    Introduction 深度增强学习Deep Reinforcement Learning是将深度学习与增强学习结合起来从而实现从Perception感知到Action动作的端对端学习的一种全新的算 ...

  10. MDN搜索结果自动跳转中文地址

    MDN社区(即Mozilla开发者社区)具有很多高质量中英文文档.它是我开发时遇到概念模糊的地方经常访问的网站.因为默认搜索一些代码,优先显示的都是英文.但是恰恰这些显示的英文文档是有中文的.每次都是 ...