JavaScript中的对象

JavaScript中的对象和java中的对象一样都是属性和方法的结合体

JavaScript提供了一些常用的内置对象,但是也可以自己创建对象

创建对象

function student(name,age) {

    this.name = name;
    this.age = age;

    this.study = function() {
        alert("studying");
    };

    this.eat = function() {
        alert("eating");
    }
}

var student1 = new student('Tom','19');

var student2 = new student('Jack','20');

访问对象的属性和方法

<script>
var x = student1.name;  // 访问对象的属性
var y = student1.age;

document.write(x);
document.write(y);

student1.study();     // 调用对象的方法
</script>

可是使用with进行反复的访问

with(student1) {
var x = name;
var y= age;
study();
eat();
}

JavaScript的内置对象

String 

var test_var = "I love You!";
document.write(test_var.length);

只有一个属性,即 length,

String 对象共有 19 个内置方法,主要包括字符串在页面中的显示、字体大小、字体颜色、字符的搜索以及字符的大小写转换等功能

Math对象

 Array 数组对象

这几个内置对象的具体方法和属性详细的去W3C上看

原文地址:https://www.cnblogs.com/wangshouchang/p/6640630.html