初学Javascript对象
<script> var p=new Object(); //属性 p.width=300; p.height=200; p.num=4; p.autotime=3; //方法 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=0;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=0;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("小明",25,"男"); //实例化 student.ShowName(); //调用对象方法
var lp = { name: "Vicky", age: 26, eat: function() { alert('1'); }, sleep: function() { alert('2'); } }; lp.sleep();
var index = 0 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(5,10); alert(obj.x); alert(obj.y); obj.FunX(); obj.FunY();
人如代码,规矩灵活;代码如诗,字句精伦。