prototypeの最上位要素のテスト
- <html>
- <head>
- <title> </title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <script src="prototype.js">
- </script>
- <script>
- var Person = Class.create();
- Person.prototype = {
- initialize: function(){
- },
- name: '',
- birthday: '',
- age: '',
- Show: function(){
- alert("This is " + this.name);
- }
- };
- function TestPerson(){
- var p = new Person();
- p.name = "Tom";
- p.age = 4;
- p.birthday = "1983-6-1";
- p.Show();
- };
- var User = Class.create();
-
- User.prototype = {
- initialize: function(){
- },
- userid: '',
- Report: function(){
- alert("UserID:" + this.userid + " Name:" + this.name + " Age:" + this.age + " Birthday:" + this.birthday);
- }
- };
- Object.extend(User.prototype, Person.prototype);
- function TestUser(){
- var user = new User();
- user.name = "Jim";
- user.age = 4;
- user.userid = 2396
- user.birthday = "1983-9-1";
- user.Show();
- user.Report();
-
- }
-
- function ShowPrototypeInfo(){
- alert(Prototype.Version + " " + Prototype.ScriptFragment);
- }
-
- function TestInspect(){
- var s = "51cto";
- alert(Object.inspect(s));
- }
-
- //-------------------------------------------------------
- function testFunctionBind(){
- var person = new Person();
- person.name = "Charli";
- person.age = 4;
- person.birthday = "1983-10-1";
- var user = new User();
- user.name = "Tom";
- user.age = 5;
- user.userid = 2396
- user.birthday = "1988-12-25";
- var handler = user.Report.bind(person);
- handler();
- }
-
- var Listener = new Class.create();
- Listener.prototype = {
- initialize: function(btn, message){
-
- $(btn).onclick = this.showMessage.bindAsEventListener(message);
- },
- showMessage: function(message){
- alert(message);
- }
- };
- var listener = new Listener("testEventListener", " !");
- </script>
- <body>
- <input type=button value="ShowPrototypeInfo" onclick='return ShowPrototypeInfo();'/> Prototype
- <br>
- <hr><input type=button value="TestPerson" onclick='return TestPerson();'/> Person p
- <br>
- <input type=button value="TestUser" onclick='return TestUser();'/>User Person , User
- <br>
- <input type=button value="TestInspect" onclick='return TestInspect();'/> Object Inspect
- <br>
- <input type=button value="testFunctionBind" onclick='return testFunctionBind();'/> Object FunctionBind
- <br>
- <input type=button value="testEventListener" id="testEventListener" />testEventListener
- <br>
- <script>
- var Listener = new Class.create();
- Listener.prototype = {
- initialize: function(btn, message){
- this.message = message;
- $(btn).onclick = this.showMessage.bindAsEventListener(this);
- },
- showMessage: function(){
- alert(this.message);
- }
- };
- var listener = new Listener("testEventListener", " !");
- </script>
- <hr>
- <script>
- function TimeExe(){
- var timerExe = new PeriodicalExecuter(showTime, 1);
-
- }
-
- function showTime(){
- var time = new Date();
- var d = $('myDiv');
- d.innerHTML = time;
- }
- </script>
- <div id="myDiv">
- <p>
- This is a paragraph
- </p>
- <input type="button" value= onclick="TimeExe();">
- <br>
- </div>
- <hr>
- <script>
- function TestNumber(){
- var n = 50;
- var b = 3;
- alert(n.toColorPart());
- alert(n.succ());
- alert(b.toPaddedString());
- //b.times(alert());
- }
- </script>
- <input type="button" value='Number ' onclick="return TestNumber();"/>
- <br>
- </body>
- </html>
本文は「喬磊のブログ学習進歩」ブログから出ており、必ずこの出典を残してください.http://sucre.blog.51cto.com/1084905/413328