初学Javascript对象

时间:2022-05-21 05:09:24
<script>
var p=new Object(); //属性
p.width=;
p.height=;
p.num=;
p.autotime=; //方法
p.autoplay=function(){
alert("play....");
}
p.test=function(){ } alert(p.width); p.autoplay();
</script>
    var test = {

        alr:function(){
var Oul = document.getElementById('ul');
var Oli = Oul.getElementsByTagName('li');
for(var i=;i<Oli.length;i++){ (function(i){
Oli[i].onclick=function(){
alert(i)
}
})(i);
}
}
} test.alr();
    function test(){

        this.alr=function(){
var Oul = document.getElementById('ul');
var Oli = Oul.getElementsByTagName('li');
for(var i=;i<Oli.length;i++){ (function(i){
Oli[i].onclick=function(){
alert(i)
}
})(i);
}
}
} var Ojc = new test(); Ojc.alr();
    //创建原型对象,定义属性、方法、及对象事件等。
Student.prototype={
_name:null,
_age:null,
_sex:null,
ShowName:function()
{
alert("Name:"+ this._name +"\n" + "Age:" + this._age + "\n" + "Sex:"+ this._sex);
}
} //专门用一个函数来初始化对象。
function Student(name,age,sex)
{
this._name=name;
this._age=age;
this._sex=sex;
} var student = new Student("小明",,"男"); //实例化
student.ShowName(); //调用对象方法
    var  lp = {
name: "Vicky",
age: ,
eat: function() {
alert('1');
},
sleep: function() {
alert('2');
}
}; lp.sleep();
    var index =
function A(x,y) {
this.x = x;
this.y = y;
A.prototype.FunX = function(){
document.body.style.background = 'red';
};
A.prototype.FunY = function(){
alert(y)
};
} var obj = new A(,);
alert(obj.x);
alert(obj.y);
obj.FunX();
obj.FunY();