1、构造函数
构造函数:就是一个构造对象的函数
一类对象的抽象
特点:
1、函数名要大写
2、指向实例化的对象(谁new,指向谁)
<script>/* 构造函数:就是一个构造对象的函数一类对象的抽象特点:1、函数名要大写2、指向实例化的对象(谁new,指向谁)*/function Person(name,age){this._name = name;this._age = age;}var p = new Person("lu",19);var li = new Person("lisi",18);console.log(p);console.log(li);/*构造对象(类) --学生 --一类事物的抽象对象 --实际的对象 --类的实例*/</script>
2、自定义函数方法
Student.prototype.Sayname = function(){
console.log(this.name)
}
Array 构造函数—数组类 /
/ 在数组的原型上定义了一个say,那么所有的实例都会拥有一个say
<script>function Student(name,age){this.name = name;this.age = age;}Student.prototype.Sayname = function(){console.log(this.name)}var p = new Student("李四",18)p.Sayname();</script>

<script>Array.prototype.push = function(){console.log("失效辣")}var arr = [];arr.push(4);console.log(arr);/* remove(value) */Array.prototype.remove = function(value){var index = arr.indexOf(value);this.splice(index,1)return this}var arr = [5,6,7];console.log(arr.remove(6))/* sum */Array.prototype.sum = function(){var s = 0;for(var i=0;i<this.length;i++){s+=this[i];}return s;}var test = [1,2,3];console.log(test.sum())</script>

1、构造函数必须通过new关键字才可以调用
2、this指向实例化的对象
<script>/*1、构造函数必须通过new关键字才可以调用2、this指向实例化的对象*/function Student(name,age,skill){this.name = name;this.age = age;this.skill = skill;}Student.prototype.SayName = function(){console.log(this.name);}Student.prototype.SaySkill = function(){console.log(this.skill);}var p = new Student("李四",18,"编程")console.log(p);p.SayName();p.SaySkill();/*类对象原型*/</script>
3、模拟Stack栈
<script>function Stack(){this.items = [];}/*入栈 push出栈 poppeek 获取栈顶isEmpty 判断栈是否为空size 可以看栈中有多少元素*/Stack.prototype.push = function(val){this.items.push(val)}Stack.prototype.pop = function(){var res = this.items.pop();return res}Stack.prototype.peek = function(){return this.items[this.items.length-1];}var s= new Stack();s.push(2);s.push(3);console.log(s.items);console.log(s.peek());</script>
4、模拟进制转换
十转二:
<script>/* 进制转换 *//* 十转二 */var num = 8;var arr = []while(num>0){arr.unshift(num%2)num = Math.floor(num/2);}console.log(Number(arr.join("")));</script>

二转十:
<script>/* 二转十 */var num = 1010;str = (num+"").split("").reverse();console.log(str);var sum = 0;for(var i =0;i<str.length;i++){sum+=str[i]*Math.pow(2,i)}console.log(sum)</script>

