JavaScript Objects
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript Objects,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3610字,纯文字阅读大概需要6分钟。
内容图文
![JavaScript Objects](/upload/InfoBanner/zyjiaocheng/745/67c78e8d4ae941ed9ef497cedfdb3c1e.jpg)
"Everything" in JavaScript is an Object: a String, a Number, an Array, a Function....
In addition, JavaScript allows you to define your own objects.
JavaScript Objects
JavaScript has several built-in objects, like String, Date, Array, and more.
An object is just a special kind of data, with properties and methods.
Accessing Object Properties
Properties are the values associated with an object.
The syntax for accessing the property of an object is:
objectName.propertyName
This example uses the length property of the String object to find the length of a string:
var message="Hello World!"; var x=message.length;
The value of x, after execution of the code above will be: 12
Accessing Objects Methods
Methods are the actions that can be performed on objects.
You can call a method with the following syntax:
objectName.methodName()
This example uses the toUpperCase() method of the String object, to convert a text to uppercase
var message="Hello world!"; var x=message.toUpperCase();
The value of x, after execution of the code above will be:
HELLO WORLD!
Creating JavaScript Objects
With JavaScript you can define and create your own objects.
There are 2 different ways to create a new object:
- 1. Define and create a direct instance of an object.
- 2. Use a function to define an object, then create new object instances.
Creating a Direct Instance
This example creates a new instance of an object, and adds four properties to it:
person=new Object(); person.firstname="John"; person.lastname="Doe"; person.age=50; person.eyecolor="blue";
Alternative syntax (using object literals):
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
Using an Object Constructor
This example uses a function to construct the object:
Example
function person(firstname,lastname,age,eyecolor) { this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; }
The reason for all the "this" stuff is that you're going to have more than one person at a time (which person you're dealing with must be clear). That's what "this" is: the instance of the object at hand.
Creating JavaScript Object Instances
Once you have a object constructor, you can create new instances of the object, like this:
var myFather=new person("John","Doe",50,"blue"); var myMother=new person("Sally","Rally",48,"green");
Adding Properties to JavaScript Objects
You can add new properties to an existing object by simply giving it a value.
Assume that the personObj already exists - you can give it new properties named firstname, lastname, age, and eyecolor as follows:
person.firstname="John"; person.lastname="Doe"; person.age=30; person.eyecolor="blue"; x=person.firstname;
The value of x, after execution of the code above will be: John
Adding Methods to JavaScript Objects
Methods are just functions attached to objects.
Defining methods to an object is done inside the constructor function:
function person(firstname,lastname,age,eyecolor) { this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; this.changeName=changeName; function changeName(name) { this.lastname=name; } }
The changeName() function assigns the value of name to the person's lastname property.
Eg:
myMother.changeName("Doe");
JavaScript Classes
JavaScript is an object oriented language, but JavaScript does not use classes.
In JavaScript you don’t define classes and create objects from these classes (as in most other object oriented languages).
JavaScript is prototype based, not class based.
转载于:https://www.cnblogs.com/Linford-Xu/archive/2013/05/08/3067253.html
内容总结
以上是互联网集市为您收集整理的JavaScript Objects全部内容,希望文章能够帮你解决JavaScript Objects所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。