<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'test3.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script src="js/jquery-2.1.1.min.js"></script> <script> //obj.getPrototypeOf根據實例對象得到原型對象 function Person(){ } Person.prototype.name='張三'; Person.prototype.age='21'; Person.prototype.sayName=function(){alert(thsi.name);} var p1=new Person(); //alert(p1.name); //var prototypeObj=Object.getPrototypeOf(p1); //alert(prototypeObj===Person.prototype); p1.name='王五'; //每次代碼讀取對象屬性時,首先會進行一次搜索,搜索實例對象裏name的屬性,看看有沒有,若是沒有, //而後再去p1的實例所對應的原型搜索,有就返回,沒有返回undefined //alert(p1.name); //delete p1.name; //alert(p1.name); //清除添加的name //得到原型對象的屬性 //判斷一個對象屬性屬於原型屬性仍是實例屬性 var p3=new Person(); var s=p3.hasOwnProperty('name'); //alert(s); //false p3.name='趙六'; //alert(p3.hasOwnProperty('name')); //true //in操做符 判斷屬性是否存在於實例對象和原型對象(沒法區分在實例仍是原型) var p4=new Person(); var p5=new Person(); //alert('name' in p4); //true p5.name='旺旺'; //alert('name' in p5); //在原型對象中是否存在屬性 function hasOwnPrototypeBySelf(object,name){ return !object.hasOwnProperty(name) && name in object; } //alert(hasOwnPrototypeBySelf(p4,'name')); //true //alert(hasOwnPrototypeBySelf(p5,'name')); //false //Object.keys()拿到當前對象中全部的屬性 var p6=new Person(); p6.sex='男'; //alert(Object.keys(p6)); //alert(Object.keys(Person.prototype)); //constructor屬性,該屬性不能被枚舉出來 //Object.getOwnPropertyNames()獲取全部屬性 不關該屬性是否能夠被枚舉 alert(Object.getOwnPropertyNames(Person.prototype)); </script> </head> <body> This is my JSP page. <br> </body> </html>