当前位置: 首页 > 编程日记 > 正文

引用类型(一):Object类型

对象表示方式

1、第一种方式:使用new操作符后跟Object构造函数

var person = new Object();<br/>
person.name = 'Nicholas';<br/>
person.age = 29;

2、对象字面量表示法
var person = {name:'Nicholas',age:29
}

*:在age属性的值29的后面不能添加逗号,因为age是这个对象的最后一个属性。在最后一个属性后添加逗号,会在 ie7及更早版本和opera中导致错误。(验证过:opera中也没啥问题),所以这个随自己

表示优劣

当需要向函数传入大量可选参数点情形。一般来来讲,命名参数虽然容易处理,但在多个可选参数点情况下就会显示不够灵活。最好的做法是对那些必需值使用命名参数,而使用对象字面量来封装多个可选参数

function displayInfo(args){var output = '';if(typeof args.name == 'string'){output = 'Name: ' + args.name + '\n';}if(typeof args.age == 'number'){output = 'Age: ' + args.age + '\n';}alert(output);
}displayInfo({name: 'Nicholas',age: 29
});
displayInfo({name:'Greg'
});

访问对象方式

访问对象时有两种表示法,点表示法和方括号表示法来访问对象的属性

alert(person['name']); //Nicholas
alert(person.name);    //Nicholas

注意:

1、方括号表示法应该将访问的属性以字符串的形式放在方括号中

2、从功能上没有任何区别。但方括号的主要优点就是可以通过变量来访问属性

var propertyName = 'name';
alert(person[propertyName]); //Nicholas

person['first name'] = 'Nicholas';

由于'first name'中包含一个空格,所以不能使用点表示法来访问它。然而,属性名中时可以包含非字母非数字点,这时候就可以使用方括号来访问它们

通常,除非必要使用变量来访问属性,否则我们建议使用点表示法

转载于:https://www.cnblogs.com/wzndkj/p/8016654.html

相关文章:

(第四周)要开工了

忙碌的一周又过去了&#xff0c;这周的时间很紧&#xff0c;但是把时间分配的比较均匀&#xff0c;考研复习和各门功课都投入了一定的精力&#xff0c;所以不像前三周一样把大多数时间都花费在了软件工程上。也因为结对项目刚开始&#xff0c;我们刚刚进行任务分工以及查找资料…

统计数字,空白符,制表符_为什么您应该在HTML中使用制表符空间而不是多个非空白空间(nbsp)...

统计数字,空白符,制表符There are a number of ways to insert spaces in HTML. The easiest way is by simply adding spaces or multiple character entities before and after the target text. Of course, that isnt the DRYest method.有多种方法可以在HTML中插入空格。…

Python20-Day02

1、数据 数据为什么要分不同的类型 数据是用来表示状态的&#xff0c;不同的状态就应该用不同类型的数据表示&#xff1b; 数据类型 数字&#xff08;整形&#xff0c;长整形&#xff0c;浮点型&#xff0c;复数&#xff09;&#xff0c;字符串&#xff0c;列表&#xff0c;元组…

Android网络框架-OkHttp3.0总结

一、概述 OkHttp是Square公司开发的一款服务于android的一个网络框架&#xff0c;主要包含&#xff1a; 一般的get请求一般的post请求基于Http的文件上传文件下载加载图片支持请求回调&#xff0c;直接返回对象、对象集合支持session的保持github地址&#xff1a;https://githu…

第一天写,希望能坚持下去。

该想的都想完了&#xff0c;不该想的似乎也已经尘埃落定了。有些事情&#xff0c;终究不是靠努力或者不努力获得的。顺其自然才是正理。 以前很多次想过要努力&#xff0c;学习一些东西&#xff0c;总是不能成&#xff0c;原因很多&#xff1a; 1.心中烦恼&#xff0c;不想学…

mac gource_如何使用Gource显示项目的时间表

mac gourceThe first time I heard about Gource was in 2013. At the time I watched this cool video showing Ruby on Rails source code evolution:我第一次听说Gource是在2013年 。 当时&#xff0c;我观看了这段很酷的视频&#xff0c;展示了Ruby on Rails源代码的演变&a…

insert语句让我学会的两个MySQL函数

我们要保存数据到数据库&#xff0c;插入数据是必须的&#xff0c;但是在业务中可能会出于某种业务要求&#xff0c;要在数据库中设计唯一索引&#xff1b;这时如果不小心插入一条业务上已经存在同样key的数据时&#xff0c;就会出现异常。 大部分的需求要求我们出现唯一键冲突…

对PInvoke函数函数调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。...

C#引入外部非托管类库时&#xff0c;有时候会出现“对PInvoke函数调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配”的报错。 通常在DllImport标签内加入属性CallingConventionCallingConvention.Cdecl即可解决该问题。 如&#xff1a; [Dll…

Python字符串方法用示例解释

字符串查找方法 (String Find Method) There are two options for finding a substring within a string in Python, find() and rfind().在Python中的字符串中有两个选项可以找到子字符串&#xff1a; find()和rfind() 。 Each will return the position that the substring …

关于命名空间namespace

虽然任意合法的PHP代码都可以包含在命名空间中&#xff0c;但只有以下类型的代码受命名空间的影响&#xff0c;它们是&#xff1a;类&#xff08;包括抽象类和traits&#xff09;、接口、函数和常量。在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句…

一 梳理 从 HDFS 到 MR。

MapReduce 不仅仅是一个工具&#xff0c;更是一个框架。我们必须拿问题解决方案去适配框架的 map 和 reduce 过程很多情况下&#xff0c;需要关注 MapReduce 作业所需要的系统资源&#xff0c;尤其是集群内部网络资源的使用情况。这是MapReduce 框架在设计上的取舍&#xff0c;…

huffman树和huffman编码

不知道为什么&#xff0c;我写的代码都是又臭又长。 直接上代码&#xff1a; #include <iostream> #include <cstdarg> using namespace std; class Node{ public:int weight;int parent, lChildren, rChildren;Node(int weight, int parent, int lChildren, int …

react 监听组合键_投资组合中需要的5个React项目

react 监听组合键Youve put in the work and now you have a solid understanding of the React library.您已经完成工作&#xff0c;现在对React库有了扎实的了解。 On top of that, you have a good grasp of JavaScript and are putting its most helpful features to use …

Unity 单元测试(PLUnitTest工具)

代码测试的由来 上几个星期上面分配给我一个装备系统,我经过了几个星期的战斗写完90%的代码. 后来策划告诉我需求有一定的改动,我就随着策划的意思修改了代码. 但是测试(Xu)告诉我装备系统很多功能都用不上了. Xu: 我有300多项测试用例,现在有很多项都无法运行了. 你修改了部分…

Best Time to Buy and Sell Stock II

题目&#xff1a; Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multipl…

求给定集合的幂集

数据结构中说这个问题可以用类似8皇后的状态树解法。 把求解过程看成是一棵二叉树&#xff0c;空集作为root&#xff0c;然后遍历给定集合中的元素&#xff0c;左子树表示取该元素&#xff0c;右子树表示舍该元素。 然后&#xff0c;root的左右元素分别重复上述过程。 就形成…

angular 命令行项目_Angular命令行界面介绍

angular 命令行项目Angular is closely associated with its command-line interface (CLI). The CLI streamlines generation of the Angular file system. It deals with most of the configuration behind the scenes so developers can start coding. The CLI also has a l…

oracle-imp导入小错filesize设置

***********************************************声明*********************************************************************** 原创作品&#xff0c;出自 “深蓝的blog” 博客。欢迎转载&#xff0c;转载时请务必注明出处。否则追究版权法律责任。表述有错误之处&#xf…

CentOS 7 下用 firewall-cmd / iptables 实现 NAT 转发供内网服务器联网

自从用 HAProxy 对服务器做了负载均衡以后&#xff0c;感觉后端服务器真的没必要再配置并占用公网IP资源。 而且由于托管服务器的公网 IP 资源是固定的&#xff0c;想上 Keepalived 的话&#xff0c;需要挤出来 3 个公网 IP 使用&#xff0c;所以更加坚定了让负载均衡后端服务器…

八皇后的一个回溯递归解法

解法来自严蔚敏的数据结构与算法。 代码如下&#xff1a; #include <iostream> using namespace std; const int N 8;//皇后数 int count 0;//解法统计 int a[N][N];//储存值的数组const char *YES "■"; const char *NO "□"; //const char *Y…

即时编译和提前编译_即时编译说明

即时编译和提前编译Just-in-time compilation is a method for improving the performance of interpreted programs. During execution the program may be compiled into native code to improve its performance. It is also known as dynamic compilation.即时编译是一种提…

bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submit][Status][Discuss]Description 给定一棵N个节点的树&#xff0c;每个点有一个权值&#xff0c;对于M个询问(u,v,k)&#xff0c;你需要回答u xor lastans和v这两个节点…

.Net SqlDbHelper

using System.Configuration; using System.Data.SqlClient; using System.Data;namespace ExamDAL {class SqHelper{#region 属性区// 连接字符串private static string strConn;public static string StrConn{get{return ConfigurationManager.ConnectionStrings["Exam&…

C语言的一个之前没有见过的特性

代码&#xff1a; #include <stdio.h>int test(){int a ({int aa 0;int bb 1;int cc 2;if(aa 0 && bb 1)printf("aa %d, bb %d\n", aa, bb);//return -2;cc;});return a; }int main(){typeof(4) a test();printf("a %d\n", a); } …

如何构建顶部导航条_如何构建导航栏

如何构建顶部导航条导航栏 (Navigation Bars) Navigation bars are a very important element to any website. They provide the main method of navigation by providing a main list of links to a user. There are many methods to creating a navigation bar. The easiest…

SPSS聚类分析:K均值聚类分析

SPSS聚类分析&#xff1a;K均值聚类分析 一、概念&#xff1a;&#xff08;分析-分类-K均值聚类&#xff09; 1、此过程使用可以处理大量个案的算法&#xff0c;根据选定的特征尝试对相对均一的个案组进行标识。不过&#xff0c;该算法要求您指定聚类的个数。如果知道&#xff…

[ 总结 ] nginx 负载均衡 及 缓存

操作系统&#xff1a;centos6.4 x64 前端使用nginx做反向代理&#xff0c;后端服务器为&#xff1a;apache php mysql 1. nginx负载均衡。 nginx编译安装&#xff08;编译安装前面的文章已经写过&#xff09;、apache php mysql 直接使用yum安装。 nginx端口&#xff1a;80…

中国剩余定理(孙子定理)的证明和c++求解

《孙子算经》里面的"物不知数"说的是这样的一个题目&#xff1a;一堆东西不知道具体数目&#xff0c;3个一数剩2个&#xff0c;5个一数剩3个&#xff0c;7个一数剩2个&#xff0c;问一共有多少个。 书里面给了计算过程及答案&#xff1a;70*2 21*3 15*2 -105*2 2…

osi七层网络层_OSI层速成课程

osi七层网络层介绍 (Introduction) Have you ever wondered how data is sent through the network from one machine to another? If yes, then the Open System Interconnected model is what you are looking for.您是否曾经想过如何通过网络将数据从一台机器发送到另一台机…