JavaScript中对象的创建有以下几种方式:
(1)使用内置对象
var str = new String("实例初始化String");
var str1 = "直接赋值的String";
var func = new Function("x","alert(x)");//示例初始化func
var o = new Object();//示例初始化一个Object
(2)使用JSON符号
var myObject = {nickname:"my girlfried",name:"big pig"};
(3)自定义对象构造
function Test()
{
this.text = function()
{
alert("defined by this");
}
}
Test.prototype.test = function()
{
alert("defined by prototype");
}
var _o = new Test();
_o.test();//输出“defined by this”