原型,原型链---

一.//什么是原型(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  检查调用的是那个函数

//构造函数 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);

  

posted on 2022-07-02 09:31  爱前端的小魏  阅读(48)  评论(0编辑  收藏  举报

导航