//创建People类 functionPeople(name,age) { this.name = name; this.age = age; } //生成对象 var fir = new People("Lisa","20"); var sec = new People("Susan","10"); var thir = new People("James","59"); var fou = new People("Li","40"); var arr2 = [fir,sec,thir,fou]; //比较函数 functioncompare() { returnfunction (a,b) { return a.age-b.age; } }; arr2.sort(compare(fir,sec)); //循环输出对象年龄 for (var i = 0; i < arr2.length; i++) { console.log(arr2[i].age); }//10,20,40,59
functionTeacher(name,sex) { this.name = name; this.sex = sex; } Teacher.prototype.say = function () { alert(this.name+":hello"); } functionStudent() { Teacher.call(this,"xiaoming","nan"); } var s = new Student(); alert(s.name); //xiaoming s.say(); //s.say() is not a function
如上,Student类只继承了Teacher类的方法和属性而没有继承原型。 当我们加上Student.prototype = new Teacher();之后,才可以继承父类的原型。
3.调用匿名函数
1 2 3 4 5 6 7 8 9 10
functiongreet() { var reply = [this.person, 'Is An Awesome', this.role].join(' '); console.log(reply); }
var i = { person: 'Douglas Crockford', role: 'Javascript Developer' };