原型
- 定义:原型是function对象的一个属性,它定义了构造函数制造出的对象的公共祖先。通过该构造函数产生的对象,可以继承该原型的属性和方法。原型也是对象。
//Person.prototype -- 原型
//Person.prototype = {} 是祖先
Person.prototype.Lastname = "Deng";
Person.prototype.say = function(){//方法
console.log('hehe');
}
function Person(name, age, sex){
this.name = name;
this.age = age;
this.sex = sex;
}
var person = new Person('xuming', 35, 'male');
var person1 = new Person();
//>person.LastName <"Deng"
- 利用原型特点和概念,可以提取共有属性。
//把公有的提取出来,放在原型里
Car.prototype = {
height : 1400,
lang : 4900,
carName : "BMW"
}
function Car(color,owner){
this.owner = owner;
this.color = color;
}
var car = new Car('red', 'prof.ji');
var car1 = new Car('green', 'laodeng');
//car Car{owner: "prof.ji", color: "red"}
//car1 Car{owner: "laodeng", color: "green"}
- 对象如何查看原型 – > 隐式属性__proto__
Person.prototype.lastName = "Deng";
function Person(name){
this.name = name;
}
var person = new Person('xuming');
person.lastName = "James";//通过对象不能修改原型属性,这里是增加属性
//{name: "xuming", lastName: "James"}
---------------分割线---------------
Person.prototype.name = 'abc';
function Person() {
//var this = {
// __proto__ : Person.prototype 连接对象和原型,存储对象的原型
//};
}
var obj = {
name : "sunny";
}
var person = new Person();
person.__proto__ = obj;
//Object{name : "sunny"}
---------------分割线---------------
Person.prototype.name = 'sunny';
function Person(){
//var this = {__proto__ : Person.prototype}
}
var person = new Person();
Person.prototype = {
name : 'cherry';
}
//var obj = {};
//var obj1 = obj;
//obj = {name : "b"};换房间了
Person.prototype = {name : "a"};
__proto__ = Person.prototype;
Persoon.prototype = {name : "b"};
---------------分割线---------------
//new之后才会生成var this = {__proto__ : Person.prototype}
Person.prototype.name = 'sunny';
function Person(){
//var this = {__proto__ : Person.prototype}
}
Person.prototype = {
name : 'cherry';
}
var person = new Person();
//Person.prototype = {name : "a"};
//Persoon.prototype = {name : "b"};
//__proto__ = Person.prototype;
原型的改:prototype.属性 = 新的属性
原型的查:prototype.属性
原型的删:不可以通过找属性找到原型
- 对象如何查看对象的构造函数 – > constructor
function Person() {
}
Car.prototype = {
constructor : Person
}//car.constructor可以手动更改
function Car() {
}
var car = new Car();
//car.constructor构造器 返回构造函数,继承的是prototype的构造器
原型链
定义和用法
当试图得到一个对象的属性时,如果这个对象本身不存在这个属性,那么就会去它构造函数的’prototype’属性中去寻找。
把原型上面加一个原型再加一个原型,把原型连成链,访问顺序依照链的顺序,叫做原型链。
原型链的连接点是__proto__,从近到远排查。
//Grand.prototype.__proto__ = Object.prototype
//Object.prototype是所有对象的最终原型
Grand.prototype.lastName = "Deng";
function Grand() {
}
var grand = new Grand();
Father.prototype = grand;
function Father() {
this.name = 'xuming';
this.fortune = {//方法
card1 : 'visa';
}
this.num = 100;
}
var father = new Father();
Son.prototype = father;
function Son() {
this.hobbit = "smoke";
}
var son = new Son();
//son.toString可以访问,返回Object的toString
son.fortune = 200;//father.fortune不变,不能赋值修改
son.fortune.card2 = 'master';//ok 引用值fortune自身的修改,调用的修改
son.num ++;//son.num 101 father.num 100
原型链上属性的增删改查
查看属性:从近到远排查,一直到原型链终端,终端要是没有就undefined
删除属性:只有自身删除
修改属性:只有自身修改,不可以赋值修改,可以调用修改
//a.sayName() sayName里面的this指向是,谁调用的这个方法,this就是指向谁
Person.prototype = {
name : "a",
sayName : function () {//方法
console.log(this.name);
}
}
function Person () {
this.name = "b";
}
var person = new Person();
---------------分割线---------------
Person.prototype = {
height : 100
}
function Person () {
this.eat = function () {//方法
this.height ++;
}
}
var person = new Person();
person.eat
//{height: 101}
person.__proto__
//{height: 100}
Object定义和用法
Object是一个构造函数,Object.create(原型)也能构造对象
var obj = {}; --> 相当于new Object()//数组/null书写规范
//var obj1 = new Object();
//obj1.__proto__ ---> Object.prototype
Person.prototype = {} --> Object.prototype
function Person() {
}
语法:Object.create(原型)
//var obj = Object.create(原型);
var obj = {name : "sunny", age : 123};
var obj1 = Object.create(obj);
obj1.name --> obj.name
---------------分割线---------------
Person.prototype.name = "sunny";
function Person() {
}
var person = Object.create(Person.prototype);
绝大多数对象的最终都会继承自Object.prototype
Object.prototype是终端,Object.create(null)是个例外
var obj = Object.create(null);//是对象,没有原型,没有__proto__
//自己加的原型__proto__没有用
//Object.create(数组);ok
.toString()方法
Object.prototype上本身含有toString方法,但输出形式是"[object Xxxxx]"
为什么数字,字符串,数组,布尔输出形式不同?
拿数字举例,尽管Number.prototype.__ proto__ = Object.prototype。但Number.prototype上有重写的toString方法,并不会调用Object.prototype本身的方法。同样:
String.prototype上有重写的toString方法
Array.prototype上有重写的toString方法
Boolean.prototype上有重写的toString方法
123.toString();//会报错,首先识别成浮点型
var num = 123;
num.toString(); //返回"123"
var str = 'abc';
str.toString(); //返回"abc"
var arr = [1,2];
arr.toString(); //返回"1,2"
var obj = {};
obj.toString(); //返回"[object Object]"
Object.prototype.toString.call(123); --> [object Number]
//123直接调用Object.toString(),不调用重写方法
重写,同样的名,不同的方法,覆盖Object.toString()
Object.prototype.toString = function() {
return 'haha';
}
Person.prototype = {
//toString : function () {
//return 'hehe';
//}
}
function Person() {
}
var person = new Person();
//person.toString() --> haha
---------------分割线---------------
var obj = Object.create(null);
document.write(obj);//调用toString方法,没有原型,不能toString,报错
obj.toString = function () {
return '老邓身体好';
}
document.write(obj);//老邓身体好
toFixed()保留两位小数(JS精度不准)
Math.ceil(123.234)向上取整
Math.floor(123.999999)向下取整
Math.random()零到一之间的随机数
可正常计算的范围,小数点前16位,后16位
for(var i = 0; i < 10; i++){
var num = Math.floor(Math.random() * 100);
console.log(num);
}
call/apply
作用,改变this指向
function Person(name, age) {
//this == obj obj代替了this
obj.name = name;
obj.age = age;
}
var person = new Person('deng', 100);
var obj = {
}
Person.call(obj, 'cheng', 300);
//第一位参数会改变this的指向(指向传入的对象),第二位以后的参数对应形参
//obj --> {name: "cheng", age: 300}
//利用别人的方法实现自己的功能
//function test() {
}
//test(); ---> test.call();
---------------分割线---------------
function Person(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
function Student(name, age, sex, tel, grade) {
//var this = {name : "", age : "", sex : ""}
Person.call(this, name, age, sex);
this.tel = tel;
this.grade = grade;
}
var student = new Student('sunny', 123, 'male', 139, 2017);
---------------分割线---------------
function Wheel(wheelSize, style) {
this.style = style;
this.wheelSize = wheelSize;
}
function Sit(c, sitColor) {
this.c = c;
this.sitColor = sitColor;
}
function Model(height, width, len) {
this.height = height;
this.width = width;
this.len = len;
}
function Car(wheelSize, style, c, sitColor, height, width, len) {
Wheel.call(this, wheelSize, style);
Sit.call(this, c, sitColor);
Model.call(this, height, width, len);
}
var car = new Car(100, '花里胡哨的', '真皮', 'red', 1800, 1900, 4900);
区别,后面传的参数形式不同
call 需要把实参按照形参的个数传进去
apply 需要传一个arguments,apply只能传数组
function Car(wheelSize, style, c, sitColor, height, width, len) {
Wheel.apply(this, [wheelSize, style]);
Sit.apply(this, [c, sitColor]);
Model.apply(this, [height, width, len]);
}
作业:
1.运行 test() 和 new test()的结果分别是什么?
var a = 5;
function test() {
a = 0;
alert(a);//0
alert(this.a);//5
var a;
alert(a);//0
}
test();
3.
a = 100;
function demo(e) {
function e() {}
arguments[0] = 2;
document.write(e);
if(a) {
var b = 123;
function c() {
//猪都能做出来
}
}
var c;
a = 10;
var a;
document.write(b);
f = 123;
document.write(c);
document.write(a);
}
var a;
demo(1);
document.write(a);
document.write(f);
4.
var str = "abc";
str += 1;
var test = typeof(str);//"string"
if(test.length == 6) {
test.sign = "typeof的返回结果可能为String";
}
document.write(test.sign);//undefined
5.function Person(name, age, sex){
var a = 0;
this.name = name;
this.age = age;
this.sex = sex;
function sss(){
a ++;
document.write(a);
}
this.say = sss;
}
var oPerson = new Person();
oPerson.say();//1
oPerson.say();//2
var oPerson1 = new Person();
oPerson1.say();//1
6.改为可以输出0-9的形式
function bb() {
var arr = [];
for(var i = 0; i < 10; i++ ) {
arr[i] = function () {
document.write(i);
}
}
return arr;
}
var arr1 = bb();
for(var i = 0; i < 10; i++ ) {
arr1[i]();
}
7.
var str = false + 1;
document.write(str);//1
var demo = false == 1;
document.write(demo);//false
if(typeof(a)&&-true + (+undefined) + "")//字符串类型
{
document.write('基础扎实');
}
if(11 + "11" * 2 == 33)
{
document.write('基础扎实');
}
!!" " + !!"" - !!false||document.write('你觉得能打印,你就是猪');
//true + false - false