js 代码
- /*
- * prototype is an attribute in Object of javascript,by attribute pototype one attribute and function can
- * contact with Object.by the way prototype attribute is working with key words 'new'
- * here is an example:
- */
- function MyObject(name,size){
- this.name = name;
- this.size = size;
- }
- MyObject.prototype.tellSize=function(){
- alert("size of "+this.name+" is "+this.size)
- }
- var MyObj = new MyObject("tiddles","7.5 meters")
- MyObj.tellSize();
========================
js 代码
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Untitled Document</title>
- <script>
- var myobject = {
- objectId:"1",
- objectname:"michael",
- getObjectName:function(){
- return this.objectname;
- },
- setObjectName:function(objname){
- this.objectname = objname;
- },
- action_shout:function(){
- alert("My name is "+this.objectname+"\nand my id is"+this.objectId);
- }
- }
- </script>
- </head>
- <body>
- <script>
- myobject.action_shout();
- myobject.setObjectName("hupeng");
- myobject.action_shout();
- </script>
- </body>
- </html>
======================
js 代码
- 内部变量可以隐式的设置
- function selectedObject(sid,sname){
- this.sid = sid;
- this.sname = sname;
- }
- var myobject2 = {
- setValues:function(selectedObject){
- this.objid = selectedObject.sid;
- this.objname = selectedObject.sname;
- },
- action_shout:function(){
- alert(this.objid+"\n"+this.objname);
- }
- }
- <script>
- var selectedobj = new selectedObject(6,"hupeng")
- myobject2.setValues(selectedobj)
- myobject2.action_shout()
- </script>