JavaScript - Test Prototype

Reference Prototype

To test a JavaScript library, you need to reference it in your web page.

To reference a library, use the <script> tag with its src attribute set to the library's URL:

<!DOCTYPE html>
<html>
<head>
<scriptsrc="http://apps.bdimg.com/libs/prototype/1.7.1.0/prototype.js"></script>
</head>
<body>
</body>
</html>


Prototype Description

Prototype provides functions that make HTML DOM programming easier.

Similar to jQuery, Prototype also has its own $() function. The

$() function accepts the id value of an HTML DOM element (or DOM element) and adds new functionality to the DOM object.

Unlike jQuery, Prototype does not have a ready() method to replace window.onload(). Instead, Prototype adds extensions to the browser and the HTML DOM.

JavaScript method:

function myFunction()
{
var obj=document.getElementById("h01");
obj.innerHTML="Hello Prototype";
}
onload=myFunction;

Prototype method:

function myFunction()
{
$("h01 ").insert("Hello Prototype!");
}
Event.observe(window,"load",myFunction);


Event.observe() accepts three parameters:

The HTML DOM or BOM (Browser Object Model) object you wish to process

The event you wish to process

You wish to Called function


Instance comparison:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<script src="http://apps.bdimg.com/libs/prototype/1.7.1.0/prototype.js"></script>
<script>
function myFunction(){
$("h01").insert("Hello Prototype!");
}
Event.observe(window,"load",myFunction);
</script>
</head>
<body>
<h1 id="h01"></h1>
</body>
</html>

Comparison:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<script src="http://apps.bdimg.com/libs/prototype/1.7.1.0/prototype.js"></script>
<script>
function myFunction(){
$("h01").writeAttribute("style","color:red").insert("Hello Prototype!");
}
Event.observe(window,"load",myFunction);
</script>
</head>
<body>
<h1 id="h01"></h1>
</body>
</html>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> function Person(name){ this.name=name; } Person.prototype.share=[]; Person.prototype.printName=function(){ alert(this.name); } var person1=new Person('Byron'); var person2=new Person('Frank'); person1.share.push(1); person2.share.push(2); console.log(person2.share); //[1,2] </script> </head> <body> <p>请在浏览器中打开 F12 观察结果</p> </body> </html>
submitReset Code