现在的位置: 首页 > 综合 > 正文

JavaScript高级程序设计_1~6章的练习

2018年05月14日 ⁄ 综合 ⁄ 共 16273字 ⁄ 字号 评论关闭

刚看完这本书的第六章,感觉很爽啊。作者的任务一是讲述知识,二是扫雷,指出容易出现的陷阱。他做到了。

//2013-8-4 18:17:03
//1,factory model
//function createPerson(name, age) {
//	var o = new Object();
//	o.name = name;
//	o.age = age;
//	o.sayName = function() {
//		alert(this.name);
//	};
//	return o;
//}
//
//var person1 = createPerson('vivi', '23');
//var person2 = createPerson('ali', '22');
// person1.sayName();
// 2,constructor model
//function Person(name, age) {
//	this.name = name;
//	this.age = age;
//	this.sayName = function() {
//		alert(this.name);
//	};
//}
//var person1 = new Person('vivi', '23');
// person1.sayName();
// alert(person1.constructor);
// alert(person1.constructor==Person);
//var x = 'x';
// alert(x.constructor);
// alert(person1 instanceof Person);
// alert(person1 instanceof Object);
// alert(x instanceof Person);
// alert(x instanceof Object);
// alert(x instanceof String);
// alert(x.constructor==String);
//var personCreate = createPerson('vivi', '23');
// alert(personCreate.constructor);
//var personCreate2 = new createPerson('vivi', '23');
// alert(personCreate2.constructor);
// alert(personCreate instanceof createPerson);
// alert(personCreate2 instanceof createPerson);
//var p1 = new Person();
//var p2 = new createPerson();
// alert(p1 instanceof Person);
// alert(p2 instanceof createPerson);
//Person('vivi', '23');
// window.sayName();
// sum(1, 3);
//function sum(num1, num2) {
//	 alert(num1+num2);
//}
//
// 5.5.3
//function callSomeFunction(someFunction, someArguments) {
//	return someFunction(someArguments);
//}
//function add3(num) {
//	return num + 3;
//}
//function getGreeting(name) {
//	return 'hello,' + name;
//}
//var result1 = callSomeFunction(add3, 33);
// alert(result1);
//var result2 = callSomeFunction(getGreeting, 'vivi');
// alert(result2);
//
//function createComparisonFunction(propertyName) {
//	return function(object1, object2) {
//		var value1 = object1[propertyName];
//		var value2 = object2[propertyName];
//		if (value1 < value2) {
//			return -1;
//		} else if (value1 > value2) {
//			return 1;
//		} else {
//			return 0;
//		}
//	};
//}
//
//var data=[{name:'zhangbin',age:2},{name:'vikram',age:11}];
//data.sort(createComparisonFunction('name'));
// alert(data[0].name);
//data.sort(createComparisonFunction('age'));
// alert(data[0].name);
//
//
//
//var message = 'vivi';
// a(message);
// a(typeof message);
// a(typeof typeof message);
// a(typeof 99);
// a(typeof typeof 99);
// a(typeof null);
// c(typeof undefined);
// c(typeof null);
// c(typeof c);
// c(typeof 'vivi');
// c(typeof 2);
// c(typeof person1);
// c(person1 instanceof Person);
// c(typeof true);
// c(2 instanceof Number);
// c(typeof '2');
// c(null == undefined);c(typeof NaN);
// c(Number.NEGATIVE_INFINITY);
// c(Number.POSITIVE_INFINITY);
// c(Number.MAX_VALUE);
// c(isNaN(2));
// c(isNaN(false));
//var num = 1024234523432234321234;
// c(num.toString(2));
// c(num.toString(3));
// c(num.toString(5));
// c(num.toString(36));
// for(var name in window){
// document.writeln(name);
// }
// for(var name in person1){
// c(name);
// }
//function sayHi(){
//	c('hello,'+arguments[0]+','+arguments[2]);
//	arguments[3] = '2';
//	c(arguments.length);
//}
// sayHi('vivi');
//function doAdd(num1,num2){
//	arguments[1]=100;
//	c(arguments[0]+num2);
//}
// doAdd(3,4);
//var colors = ['red','black','blue'];
// c(colors.length);
// colors[99]='brown';
// c(colors.length);
// c(colors[55]);
// c(colors instanceof Object);
// c(colors instanceof Array);
// c(typeof colors);
// c(colors instanceof String);
// c(Array.isArray(colors));
// c(colors);
// colors.toString();
// c(colors);
// c(colors.toLocaleString());
// c(colors.join('*'));
// var colors = new Array();
// var count = colors.push('red','black');
// var count = colors.push('green');
// var item = colors.pop();
// c(item);
// c(count);c(colors);
// c(colors.length);
// var item = colors.shift();
// c(item);c(colors.length);
// var values = [1,5,10,15];
// c(values.sort());
// c(values.sort(function(){
//	
// }));
//var colors = ['red','black','white'];
// var colors2 = colors.concat('pink');
// c(colors2);
// var colors3 = colors2;
// colors3.pop();
// c(colors3);
// c(colors2);
// c(colors);
//var removed = colors.splice(0, 1);
// c(removed);
// c(colors);
//removed = colors.splice(2, 0, 'yellow','orange');
// c(removed);
// c(colors);
//removed=colors.splice(2, 2, 'red','purple');
// c(colors);
// c(removed);
// c(colors);
// 2013-8-4 18:17:03
// var numbers = new Array();
// numbers = [1,2,3,4,5,4,3,2,1];
// c(numbers.indexOf(2));
//var numbers=[1,2,3,4,5,4,3,2,1];
// c(numbers.indexOf(8));
// c(numbers.indexOf(4,4));
//var person = {'name':'vivi'};
//var people=[{'name':'vivi'}];
//var morePeople = [person];
// c(morePeople);
// c(people.indexOf(person));
// c(morePeople.indexOf(person));
// c(people instanceof Array);
//var everyResult=numbers.every(function (item,index,array){
// c(arguments[0]);
// c(arguments[1]);
// c(arguments[2]);
// c(arguments[3]);
// c('-----');
// return (item>2);
//	return (item*2);
//});
// c(everyResult);
//var someResult=numbers.some(function(item,index,array){
//	return item > 2;
//});
// c(someResult);
//var filterResult = numbers.filter(function(item,index,array){
//	return item > 2;
//});
// c(filterResult);
//var mapResult=numbers.map(function(item,index,array){
// return item*2;
//	return item >2;
//});
// c(mapResult);
//
//var forEachResult=numbers.forEach(function(item,index,array){
// return c(item);
//});
// c(forEachResult);
// var values=[1,2,3,4,5];
//var values=[1];
// var sum=values.reduce(function(prev,cur,index,array){
//  a('in reduce');
//  c(prev + ',' + cur);
// return prev + cur;
// });
// c(sum);
//var now = new Date();
// c(now);
// c(now.toString());
// c(now.toDateString());
// c(now.toLocaleDateString());
// c(now.toLocaleString());
// c(now.toTimeString());
// c(now.toUTCString());
// for(var name in now){
// c(name);
// }
// c(now instanceof Date);
// for(var name in Date){
// c(name);
// }
// var t = new Date(Date.parse('222'));
//var t = new Date(3002);
// c(t.toLocaleString());
//var pattern2 = new RegExp('[bc]at','i');
// c(pattern2.global);
// c(pattern2.ignoreCase);
//var text = 'mom and dad and baby';
//var pattern=/mom( and dad( and baby)?)?/gi;
// c(pattern instanceof RegExp);
//var matches=pattern.exec(text);
// c(matches);
//var text = 'cat,bat,sat,fat,lat,dat,sat';
//var pattern1=/.at/;
// var matches=pattern1.exec(text);
// c(matches);
// c(pattern1.lastIndex);
//var pattern2=/.at/g;
// var matches=pattern2.exec(text);
// c(matches);
// matches=pattern2.exec(text);
// c(matches);
// var matches2 = ['ds'];
// while(pattern2.test(text)){
// c(pattern2.lastIndex);
// var x = pattern2.exec(text);
// c('x:' + x);
// matches2.push(x[0]);
// matches2.push('red');
//  c(matches2);
// }
// c(matches2);
// 2013年8月4日22:30:30
// function factorial(num){
// c(arguments.callee);
// c('num:'+num);
// if(num<1){
// return 1;
// }else{
// return num*factorial(num-1);
// }
// }
// var num = factorial(5);
//function factorial(num){
//	c(arguments.callee);
//	if(num<1){
//		return 1;
//	}else{
//		return num* arguments.callee(num-1);
//	}
//}
// var num = factorial(5);
// var trueFactorial=factorial;
// var num=trueFactorial(5);
//function outer(){
//	inner();
// c(outer.caller);
//}
//function inner(){
//	c(inner.caller);
//	c(arguments.callee.caller);
//}
// outer();
// c(outer.caller);
//
//function sum(num1,num2){
//	return num1 + num2;
//}
//function callSum1(num1,num2){
//	c(this.text);
//	c(this);
//	c(arguments);
//	return sum.apply(this,[num1,num2]);
//}
// c(callSum1(10,22));
//function callSum2(num1,num2){
//	this.name='haha';
//	var aa = callSum1(22,33);
//}
// callSum2();
// c(aa);
// c(name);
//window.color='red';
//var o={color:'blue'};
//function sayColor(){
//	c(this);
//	c(this.color);
//}
// sayColor();
// sayColor.call(this);
// sayColor.call(window);
// sayColor.call(o);
// c.sayColor();
//var objectSayColor=sayColor.bind(o);
// c(objectSayColor);
// objectSayColor();
// for(var name in Global){
// c(name);
// }
//var uri='a b  c我';
// c(encodeURI(uri));
//var msg = 'hello world';
// eval('c(msg)');
//var person = {};
//Object.defineProperty(person,'name',{
//	writable:false,
//	value:'vivi'
//});
// c(person.name);
//person.name='ali';
// c(person.name);
//var person={
//		name:'kakui'
//};
// c(person.name);
// 2013-8-6 22:24:48
//var book = {
//		_year :2004,
//		edition:1
//};
//
//Object.defineProperty(book,'year',{
//	get:function(){
//		a('get function run.');
//		return this._year;
//	},
//	set:function(newValue){
//		a('set function run.');
//		if(newValue > 2004){
//			this._year = newValue;
//			this.edition += newValue - 2004;
//		}
//	}
//});
//
// book.year = 2005;
// alert(book.edition);
// alert('book.getYear:' + book.year);
//
//var book = {};
//
//Object.defineProperties(book,{
//	_year : {
//		value : 2004
//	},
//	
//	edition : {
//		value : 1
//	},
//	
//	year : {
//		get : function(){
//			return this._year;
//		},
//		set : function(newValue){
//			if(newValue > 2004){
//				this._year = newValue;
//				this.edition = newValue-2004;
//			}
//		}
//	}
//});
//
//var descriptor = Object.getOwnPropertyDescriptor(book,'_year');
// for(var name in descriptor){
// c(name + ':'+descriptor[name]);
// }
//
//var person1 = new Person('vivi',22);
// c(person1.constructor);
// a(person1.constructor);
//
//var person2 = new Person('vivivivi',22);
// c(person2 instanceof Person);
// c(person1 instanceof Person);
// c(person2.sayName());
// c(window.sayName());
//
//function Person(){
//	this.other = 'other';
//	this.sayOther = function(){
//		alert(this.other);
//	};
//}
//
// a(Person.prototype);
// c(Person.prototype);
//
//Person.prototype.name = 'vivi';
//Person.prototype.age = 22;
//Person.prototype.sayName = function(){
//	alert(this.name);
//};
//Person.prototype.sayOther = function(){
//	alert('othor function in proto');
//};
//
// a(Person.prototype);
// c(Person.prototype);
//
//var person1 = new Person();
//var person2 = new Person();
// c(person1.name);
// c(person2.name);
// c(person1.sayName == person2.sayName);
// c(person1.other == person2.other);
// c(person1.sayOther == person2.sayOther);
// a(person1.constructor);
// a(Person.constructor);
// c(person1.constructor == Person.constructor);
// c(person1.constructor);
// c(Person.constructor);
// a(Person.prototype);
// c(person1.__proto__);
// c(Person.prototype.constructor == person1.constructor);
// c(Person == Person.prototype.constructor);
// c(Person.prototype.isPrototypeOf(person1));
// c(name.isPrototypeOf(person1));
// c(Object.getPrototypeOf(person1));
// person1.sayOther();
//
// 2013-8-7 22:56:57
//function Person(){}
//Person.prototype.name = 'vivi';
//Person.prototype.age = 22;
//Person.prototype.sayName = function(){alert(this.name);};
//
//var person1 = new Person();
//var person2 = new Person();
//
// c(person1.hasOwnProperty('name'));
//person1.name='ali';
// c(person1.hasOwnProperty('name'));
// delete person1.name;
// c(person1.name);
// c(person1.hasOwnProperty('name'));
// c(Object.getOwnPropertyDescriptor(person1,'age'));
// c('name' in person1);
//function hasPrototypeProperty(object,name){
//	return !object.hasOwnProperty(name)&&(name in object);
//}
//var keys = Object.keys(Person.prototype);
// c(keys);
//keys = Object.keys(person1);
// c(keys);
//keys = Object.keys(person1.constructor);
// c(keys);
// c(Person.prototype instanceof Object);
//var keys = Object.getOwnPropertyNames(Person.prototype);
// c(keys);
//
//function Person(){}
//Person.prototype = {
//		name:'vivi',
//		age:22,
//		sayName : function(){
//			alert(this.name);
//		}
//};
// a(Person.prototype.constructor);
//var friend = new Person();
// c(friend instanceof Person);
//var friend2 = Person();
// c(friend2 instanceof Person);
// c(friend2 instanceof Object);
// c(friend2);
//Object.defineProperty(Person.prototype,'constructor',{
//	enumerable :false,
//	value : Person
//});
//
//function Person2(){}
//var friend = new Person2();
//Person2.prototype = {
//		construtor : Person,
//		name:'vivi',
//		age:22,
//		sayName : function(){
//			alert(this.name);
//		}
//};
//
// friend.sayName();
// c(friend.name);
//var keys = Object.keys(Object.prototype);
// c(keys);
//keys = Object.keys(Array.prototype);
// c(keys);
// c(typeof Array.prototype.sort);
//keys = Object.getOwnPropertyNames(Object.prototype);
// c(keys);
//keys = Object.getOwnPropertyNames(person1);
// c(keys);
// c(person1.__proto__);
//
//String.prototype.startsWith = function(text){
//	return this.indexOf(text) == 0;
//};
//
//var msg = 'Hello World!';
// c(msg.startsWith('hello'));
//function Person(){}
//
//Person.prototype = {
//		name:'vivi',
//		age:22,
//		friends:['jl','ali'],
//		sayName : function(){
//			alert(this.name);
//		}
//};
//
//var person1 = new Person();
//var person2 = new Person();
//person1.friends.push('xj');
// c(person2.friends);
//
//function Person(name,age){
//	this.name = name;
//	this.age = age;
//	this.friends = ['ali','xiaoxi'];
//}
//
//Person.prototype = {
//		constructor : Person,
//		sayName : function(){
//			alert(this.name);
//		}
//};
//
//function Person(name,age){
//	this.name = name;
//	this.age = age;
//	if(typeof sayName != 'function'){
//		Person.prototype.sayName = function(){
//			alert(this.name);
//		};
//	}
//}
//
//2013-8-8 22:42:38
//function SpecialArray(){
//	var values = new Array();
//	values.push.apply(values,arguments);
// values.push(arguments[0],arguments[1]);
//	values.toPipedString = function(){
//		return this.join('|');
//	};
//	return values;
//}
//
//var colors = new SpecialArray('red','blue','black');
// c(colors);
//c(colors.toPipedString());
//var time = new Date();
// for(var prop in Object.getOwnPropertyNames(time)){
// c(prop);
// }
//
//function SuperType(){
//	this.property = true;
//}
//
//SuperType.prototype.getSuperValue = function(){
//	return this.property;
//};
//
//function SubType(){
//	this.subproperty = false;
//}
//
//SubType.prototype = new SuperType();
//SubType.prototype.getSubValue = function(){
//	return this.subproperty;
//};
//
//var instance = new SubType();
//c(instance.getSuperValue());
//c(instance.constructor);
//
//c(instance.__proto__.constructor);
//c(instance instanceof SubType);
//
//c(Object.prototype.isPrototypeOf(instance));
//c(SuperType.prototype.isPrototypeOf(instance));
//c(SubType.prototype.isPrototypeOf(instance));
//
//function SuperType(){
//	this.property = true;
//}
//
//SuperType.prototype.getSuperType = function(){
//	return this.property;
//};
//
//function SubType(){
//	this.subproperty = false;
//}
//
//SubType.prototype = new SuperType();
//
//SubType.prototype.getSubValue = function(){
//	return this.subproperty;
//};
//
//SubType.prototype.getSuperValue = function(){
//	return false;
//};
//
//var instance = new SubType();
//c(instance.getSuperValue());
//
//function SuperType(){
//	this.property = true;
//}
//
//SuperType.prototype.getSuperValue = function(){
//	return this.property;
//}
//
//function SubType(){
//	this.subproperty = false;
//}
//
//SubType.prototype.getSubValue = function(){
//	return this.subproperty;
//};
//
//SubType.prototype = {
//		getValues : function(){
//			
//		}
//};
//
//c(SubType.prototype);
//
//
//var instance = new SubType();
//c(SubType instanceof Function);
//c(SubType.hasOwnProperty('prototype'));
//c(instance.hasOwnProperty('prototype'));
//c(instance.hasOwnProperty('__proto__'));
//c(instance.__proto__);
//
//function SuperType(){
//	this.colors = ['red','green','black'];
//}
//
//function SubType(){}
//
//SubType.prototype = new SuperType();
//
//var instance1 = new SubType();
//instance1.colors.push('grey');
//c(instance1.colors);
//var instance2 = new SubType();
//c(instance2.colors);
//var instance3 = new SuperType();
//c(instance3.colors);
//
//function SuperType() {
//	this.colors = [ 'red', 'black' ];
//}
//
//function SubType() {
//	SuperType.call(this);
//}
//
//var instance1 = new SubType();
//instance1.colors.push('green');
//c(instance1.colors);
//var instance2 = new SubType();
//c(instance2.colors);
//c(instance1.__proto__);
//c(instance1.constructor);
//
//function SuperType(name){
//	this.name = name;
//}
//
//function SubType(name){
//	SuperType.call(this, arguments);
//}
//
//var instance1 = new SubType('vivi');
//c(instance1.name);

//function SuperType(name) {
//	this.name = name;
//	this.colors = [ 'red', 'black' ];
//}
//
//SuperType.prototype.sayName = function() {
//	alert(this.name);
//};
//
//function SubType(name, age) {
//	SuperType.call(this, name);
//	this.age = age;
//}
//
//SubType.prototype = new SuperType();
//SubType.prototype.sayAge = function(){
//	alert(this.age);
//};
//
//var instance1 = new SubType('vivi',22);
//instance1.colors.push('green');
//c(instance1.colors);
//instance1.sayName();
//instance1.sayAge();
//var instance2 = new SubType('ali',23);
//c(instance2.colors);
//instance2.sayName();
//instance2k.sayAge();

function object(o) {
	function F() {
	}
	F.prototype = o;
	return new F();
}

var person = {
	name : 'vivi',
	friends : [ 'ali', 'aming' ]
};
//
// c(person.frends);
// var anotherPerson = object(person);
// anotherPerson.name = 'xiaoxi';
// anotherPerson.frends = [];
// anotherPerson.frends.push('xiaozhen');
// var yetAnotherPerson = object(person);
// yetAnotherPerson.name='jiuli';
// yetAnotherPerson.frends.push('ahui');
// c(person.frends);
// c(anotherPerson.frends);
// c(yetAnotherPerson.frends);
// c(person.name);
// c(anotherPerson.name);
// c(anotherPerson.__proto__.frends == person.frends);
//
// var anotherPerson = Object.create(person);
// anotherPerson.name = 'xiaozhen';
// anotherPerson.friends.push('xiaoxi');
// c(person.friends);
// c(person.name);

function createAnother(original) {
	var clone = object(original);
	clone.sayHi = function() {
		alert('hi');
	};
	return clone;
}

function inheritPrototype(subType, superType) {
	var prototype = superType.prototype;
	subType.prototype = prototype;
	subType.constructor = subType;
}

function SuperType(name) {
	this.name = name;
	this.colors = [ 'red', 'black' ];
}

SuperType.prototype.sayName = function() {
	alert(this.name);
};

function SubType(name, age) {
	SuperType.call(this, name);
	this.age = age;
}

inheritPrototype(SubType, SuperType);

SubType.prototype.sayAge = function() {
	alert(this.age);
};

var instance1 = new SubType();
instance1.colors.push('green');
c(instance1.colors);
var instance2 = new SuperType();
c(instance2.colors);

抱歉!评论已关闭.