两种创建对象方式:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <script> /*-----------方式一①-----------*/ //people = new Object(); //people.name = "Lee"; //people.age = 27; /*-----------方式一②-----------*/ //people = { name: "Ski", age: 26 }; /*-----------方式二(函数定义对象)-----------*/ function peopleFun(name, age) { this.name = name; this.age = age; } people =new peopleFun("Lee.Ski", 26.5); people2 = new peopleFun("Lee.SkII", 27); document.write("name:" + people.name + ",age:" + people.age); document.write("name:" + people2.name + ",age:" + people2.age); </script> </body> </html>
内置对象一般会提供已经定义好的属性和方法使用:
示例代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <script> var str = "Hello world"; //document.write(str.length); //document.write(str.indexOf("world")); //document.write(str.match("world"));//匹配到了则返回world,否则返回null //document.write(str.replace("world","Ski")); //document.write(str.toLowerCase()); var str1 = "1,2,3,4,5,6"; //document.write(str1.split(",")); var strArr = str1.split(","); for (var i = 0; i < strArr.length; i++) { document.write(strArr[i]+"<br/>"); } </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <script> var arr1 = ["happy", "time", "bir"]; var arr2 = [6, 20, 0.3, 4, 5]; //document.write(arr1.concat(arr2)); //document.write(arr2.sort((a, b) => a - b)); //document.write(arr1.push("TT")); document.write(arr1.reverse()); </script> </body> </html>
End