[jQuery Note]jQuery Event Handle-JavaScript Object fundamentals

At first,If you are greate deeply in studying the JavaScript.You can take this artical as a little case.

 
Now,we are going to concentrate on the fundamental concepts that we need to make the most effective use of jQuery in our web applications.
 
 
The most important of these concepts is that functions are first-class objects in JavaScript,which is a result of the way JavaScript defines and deals with functions.
 
 
Before we catch this scence,we must make sure that we understand what a JavaScript object is all about.
So,dive right in.
 
 
A new object comes into existence via the new operator paired with the Object constructor.Creating an object is as easy as this:
 
var aNewObject = new Object();
 
Yes,most of the majority object-oriented languages var object like that,and as a result,the created object,for example,aNewObject created in this case,would have this Object's attributes,maybe include name,age,sex and so on.At the same time,we may use setter and getter methods to operate the object.
But,we must declare the methods or the variables before we use them.
 
However,JavaScript allows us to create them dynamically as we need.
 
We may write our code like this(first type):
 
var student = new Object();
student.name='XiaoMing';
student.age=18;
student.birth=new Date(1994,7,9);
student.sex='boy';
 
Here we create a new Object instance and assign it to a variable named student.We often populate this varialbe with a number of properties of different types.Also,we don't need to declare these properties prior to assigning them;They come into being merely by the act of our assigning a value to them.
 
But we must pay more attention to the mistakes that we will make.For example:
I want to change the student's age value,and I write my code like the words below
 
student.aeg:20
 
There's no compiler to warn us that we've made a mistake;a new property named aeg is cheerfully created on our behalf,leving us to wonder later why the new age didn't take when we refference the correctly spelled property. So,with greate power comes greate responsibility.
 
From this example,we can conclude that,the primary purpose of an Object instance is to serve as a container for a named collection of other objects.That may remaind you of concepts in other language:
A Java map or hashTable or hashMap or dictionary in other language.
 
What's more, An object property can be another object instance,which can in turn be objects with their own properties,and so on.
 
Let's extend the example above,
 
var classDetail =new Object();
classDetail.name='Class A';
classDetail.grate='3';
classDetail.No='5';
classDetail.studentInf=student;
 
We can access the student's name by writting like this:
var studentName=classDetail.studentInf.name;
 
Also we can write like this:
 
var studentName=classDetail['studentInf']['name'];
 
But also ,we can write lilke this:
 
var studentName=classDetail['student'+'Inf']['na'+'me'];
 
why shoud I splite the words,because you can build the string dynamically.
 
However,if a property name doesn't declare before use,for example:
 
classDetail['NoNameProperty'];
 
It will return undefine.
 
As a conclution,the more general format for accessing properties is 
 
object[propertyNameexpression]
 
Consider the following statement:
 
var classDetail={
     name:'Class A',
     grate:'3',
     No:'5',
     studentInf:{
          name:'XiaoMing',
age:18,
birth:new Date(1994,7,9),
sex:'boy';
     }
};
 
This writting style has come to be termed JSON(JavaScript Object Notation),is much preferred by most page authors over the multiple-assignment means of object building.

转载于:https://www.cnblogs.com/Xuhaiyang/archive/2012/07/12/2588420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值