var person = { name: "飞林沙", age: 21, Introduce: function () { alert("My name is " + this.name + ".I'm " + this.age); } }; person.Introduce(); script>
这个是不是很像我们在C#3.0里提出的匿名对象呢? 代码如下: protected void Page_Load(object sender, EventArgs e) { var person = new { name = "飞林沙", age = 21 }; Response.Write("My name is " + person.name + ".I'm " + person.age); }
var Person = function () { this.name = "飞林沙"; this.age = 21; this.Introduce = function () { alert("My name is " + this.name + ".I'm " + this.age); }; }; var person = new Person(); person.Introduce(); script>
var Person = function (name, age) { this.name = name; this.age = age; this.Introduce = function () { alert("My name is " + this.name + ".I'm " + this.age); }; }; var person = new Person("飞林沙", 21); person.Introduce(); script>
var Person = function (name, age) { this.name = name; this.age = age; this.Introduce = function () { alert("My name is " + name + ".I'm " + age); }; }; var person = new Person("飞林沙", 21); for (var p in person) { alert(p); } alert(person["name"]); script>
var Person = function (name, age) { var name = name; var age = age; this.GetName = function () { return name; } this.GetAge = function () { return age; } this.Introduce = function () { alert("My name is " + name + ".I'm " + age); }; }; var person = new Person("飞é林?沙3", 21); alert(person["name"]); alert(person.GetName()); script>