|
| 1 | +// window.onload = function(){ |
| 2 | +// var obj = {x: 4}; |
| 3 | +// console.log(obj.x); |
| 4 | +// } |
| 5 | + |
| 6 | +var obj = {x: 431}; |
| 7 | +console.log(obj.x); |
| 8 | + |
| 9 | +// .instanceof运算符: 此运算符可以判断一个变量是否是某个对象(类)的实例,返回值是布尔类型的 |
| 10 | +var str = new String("antzone"); |
| 11 | +console.log(str instanceof String); |
| 12 | + |
| 13 | +//typeof运算符:此运算符可以返回一个字符串,用语说明元算数的类型 |
| 14 | +var str = new String("antzone"); |
| 15 | +var strTwo = "antzone"; |
| 16 | +console.log(typeof str); |
| 17 | +console.log(typeof strTwo); |
| 18 | + |
| 19 | +// use strict : 声明严格模式 |
| 20 | + |
| 21 | +//原型链 |
| 22 | +var obj2 = {x: 111, y: 222, z: 333}; |
| 23 | +console.log(obj2, obj2.y); |
| 24 | +delete obj2.y; |
| 25 | +console.log(obj2); |
| 26 | + |
| 27 | +// 属性 get set 方法 |
| 28 | +// delete 删除属性 |
| 29 | +var cat = new Object(); |
| 30 | +cat.legs = 4; |
| 31 | +cat.name = "kitty"; |
| 32 | +console.log(cat); |
| 33 | +if (cat.hasOwnProperty('legs')) { |
| 34 | + console.log("has legs"); |
| 35 | +} |
| 36 | +var man = { |
| 37 | + age: 28, |
| 38 | + weibo: '@Bosd' |
| 39 | +} |
| 40 | +console.log(typeof cat); |
| 41 | +console.log(typeof man); |
| 42 | + |
| 43 | +// 对象标签、序列化 |
| 44 | +// proto:原型标签 |
| 45 | +// class标签 |
| 46 | +// extensible表示对象是否可扩展 |
| 47 | + |
| 48 | +var obj3 = {x: 1, y: true, z:[1, 2, 3], nullVal: null}; |
| 49 | +var json = JSON.stringify(obj3); |
| 50 | +console.log(obj3); |
| 51 | +console.log(json); |
| 52 | + |
| 53 | +// OOP |
| 54 | +// 基于原型的继承 |
| 55 | +// prototype:对象上预设的原型属性 |
| 56 | +function Fool(){ |
| 57 | + this.x = 1; |
| 58 | +} |
| 59 | + |
| 60 | +Fool.prototype.x = 2; |
| 61 | +console.log(Fool.x); |
| 62 | +console.log(Fool.prototype.x); |
| 63 | + |
| 64 | +// 测试 |
| 65 | +function Person(name, age){ |
| 66 | + this.name = name; |
| 67 | + this.age = age; |
| 68 | +} |
| 69 | +console.log("Person class:" + typeof Person); |
| 70 | + |
| 71 | +Person.prototype.hi = function(){ |
| 72 | + console.log("Hi my name is" + this.name + "i am" + this.age + "years old now"); |
| 73 | +} |
| 74 | +Person.prototype.LEGS_NUM = 2; |
| 75 | +Person.prototype.ARMS_NUM = 2; |
| 76 | +Person.prototype.walk = function(){ |
| 77 | + console.log(this.name + "is walking"); |
| 78 | +} |
| 79 | + |
| 80 | +function Student(name, age, className) { |
| 81 | + // call调用父类 |
| 82 | + Person.call(this, name, age); |
| 83 | + this.className = className; |
| 84 | +} |
| 85 | + |
| 86 | +// 继承Person的prototype方法 |
| 87 | +// Object.create创建空对象,并且空对象的原型指向Persion;避免直接赋值时给student原型增加属性时Person也跟着增加的错误 |
| 88 | +Student.prototype = Object.create(Person.prototype); |
| 89 | +// constructor指向student本身,如果不设置的话constructor会指向Person |
| 90 | +Student.prototype.constructor = Student; |
| 91 | + |
| 92 | +Student.prototype.hi = function(){ |
| 93 | + console.log("Hi my name is" + this.name + "i am" + this.age + "years old now" + "and from" + this.className); |
| 94 | +} |
| 95 | + |
| 96 | +Student.prototype.learn = function(subject){ |
| 97 | + console.log("Hi my name is" + this.name + "is learning" + subject + "at" + this.className + "."); |
| 98 | +} |
| 99 | + |
| 100 | +var bosn = new Student("Bosn", 29, "Class3,Grade 2"); |
| 101 | +console.log("new obj type" + typeof bosn); |
| 102 | +bosn.hi; |
| 103 | +bosn.LEGS_NUM; |
| 104 | +bosn.walk(); |
| 105 | +bosn.learn('math'); |
| 106 | + |
| 107 | + |
| 108 | + |
0 commit comments