码迷,mamicode.com
首页 > Web开发 > 详细

JS(一)基础知识与对象

时间:2015-02-12 18:28:55      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:javascript   对象   

一、简介

在JS中,一切皆为对象。字符串、数组、函数等都是对象。

二、常见的js功能

/**
 * 显示对象的属性方法一
 * @returns
 */
function myFunction1(){
	person = {firstname:"David",lastname:"Smith",age:30,sex:'男'};//每一个都是一个新对象,属性值可以不固定
	//person2 ={...};
	printInfo(person);
}
/**
 * 显示对象的属性方法二
 */
function myFunction2(){
	person = new Object();
	person.firstname ="David";
	person.lastname = "Smith";
	person.age = 30;
	person.sex = '男';
	printInfo(person);
}
/**
 *构造函数
 * @returns
 */
function Person(firstname,lastname,age,sex){
	this.firstname = firstname;
	this.lastname= lastname;
	this.age = age;
	this.sex = sex;

	this.test = test2;//这个一定不能少
	function test2(){
		document.write("调用了Person的test()");
		//alert("调用了Person的test()");
	}
}
/**
 * 显示对象的属性方法三
 */
function myFunction3(){
	var p = new Person("David","Smith",30,'男');
	//
	p.test();
	//
	//printInfo(p);
}
/**
 * 遍历Person对象的属性
 */
function traversalPerson(){
	//这里如果要遍历的话,不能使用构造函数创建对象,
	var p = {firstname:"David",lastname:"Smith",age:30,sex:'男'};
	var str="";
	var x;
	for(x in p){
		str += p[x]+",";//JS中用 "+=",php中用".="
	}
	//document.write(str);
	document.getElementById("div1").innerHTML=str;
}
/********************通用的方法******************************/
/**
 * 输出人物信息
 */
function printInfo(person){
	document.getElementById("div1").innerHTML="姓名:" + person.firstname + " " + person.lastname + ",年龄:"+person.age+",性别:" + person.sex;
}













JS(一)基础知识与对象

标签:javascript   对象   

原文地址:http://blog.csdn.net/z18789231876/article/details/43764951

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!