JS(一)基礎知識與對象

來源:互聯網
上載者:User

JS(一)基礎知識與對象

一、簡介

在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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.