天天看點

原型,原型鍊---

一.//什麼是原型(prototype):函數的祖先,函數繼承原型的一切

//如下:
//控制台進行列印
// Preson.prototype.name = "wsx";
// function Preson () {

// }
// //輸出 pso.name 列印出 : wsx
// var pso = new Preson();      

二.//提取共有屬性(增删改查)

//Preson.prototype.sex = "222000"; 修改屬性值
//Preson.prototype.nature = 'male'; //增加
//deleter pre.name //删除

// Preson.prototype.sex = '18';
// function Preson(name,age) {
// this.name = name;
// this.age = age;
// }
// var pre = new Preson('name',"13");      
//構造函數 constructor
// function Person() {

// }

// Car.prototype = {
// constructor : Person
// }
// function Car () {

// }
// var car = new Car();



function Person () {

}
var a = new Person();//控制台輸入:a.constructor 列印出:被調用的函數      
//原型鍊:最高層級為object
         //如下

   //          Infor.prototype.name = "wsx:基本資訊";
         // function Infor () {
            
         // }
         // var infor = new Infor();
         
         // Hobby.prototype = infor;
         // function Hobby() {
         //    this.hobby = "喝酒";
         //    this.number = {
         //       case : "A",
         //       case1 : "B"
         //    }
         // }
         // var hobby = new Hobby();
         
         // Age.prototype = hobby;
         // function Age() {
         //    this.age = "20";
         // }
         // var age = new Age(); 
         
         
         //誰用this這個方法,this就是誰。 //魏XX
         //第一題
         // Test.prototype = {
         //    name : "wsx",
         //    sam : function() {
         //       console.log(this.name);
         //    }
         // }
         //  function Test() {
         //    this.name = "魏XX";
         //  }
         // var test = new Test();
         
         
         
         //第二題
         // Test.prototype =  {
         //    height : 100
         // }
         
         // function Test() {
         //    this.eat = function () {
         //       this.height ++;
               
         //    }
            
         // }     
         
         //var test = {};  //一般使用第一種寫法。字面量
         //var test = new Test();
         
         //object.create 不會繼承Object.prototype 其他對象會繼承  隻可以輸入null和原型
         //null和undefined不會繼承tostring方法
         // Test.prototype.name = "wsx";
         //  function  Test() {
         //    console.log('111');
         //  }
         // var obj = Object.create(Test.prototype);//      No properties
         
         //重寫tostring值   
         //輸出的是null的值,列印出“魏”
         // var test = Object.create(null);
         // test.toString = function () {
         //    return '魏';
         // }
         // document.write(test);      

繼續閱讀