这里有新鲜出炉的 Javascript 教程,程序狗速度看过来!
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
这篇文章主要介绍了 js 初始化验证, 结合实例形式分析了 javascript 初始化验证相关的判断、设置、检测等相关操作技巧, 需要的朋友可以参考下
本文实例讲述了 js 初始化验证的方法。分享给大家供大家参考,具体如下:
- <script type="text/javascript">
- var Book = function(isbn, title, author) {
- if(!this.checkIsbn(isbn)){
- throw new Error('Book: Invalid ISBN.');
- }
- this.isbn = isbn;
- this.title = title || 'No title specified';
- this.author = author || 'No author specified';
- }
- Book.prototype = {
- checkIsbn: function(isbn) {
- if(isbn == undefined || typeof isbn != 'string') {
- return false;
- }
- return true; // All tests passed.
- },
- display: function() {
- alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author);
- }
- };
- var theHobbit = new Book('0-395-07122-4', 'The Hobbit', 'J. R. R. Tolkein');
- theHobbit.display(); // Outputs the data by creating and populating an html element.
- </script>
对 isbn 进行验证。是否定义,是否为字符串等等。对 title 进行判断,设置默认。
另一种实现方式
- <script type="text/javascript">
- /* 出版 interface. */
- /* var Publication = new Interface('Publication', ['getIsbn', 'setIsbn', 'getTitle',
- 'setTitle', 'getAuthor', 'setAuthor', 'display']); */
- var Book = function(isbn, title, author) { // implements Publication
- this.setIsbn(isbn);
- this.setTitle(title);
- this.setAuthor(author);
- }
- Book.prototype = {
- checkIsbn: function(isbn) {
- if(isbn == undefined || typeof isbn != 'string') {
- return false;
- }
- return true; // All tests passed.
- },
- getIsbn: function() {
- return this.isbn;
- },
- setIsbn: function(isbn) {
- if(!this.checkIsbn(isbn)) throw new Error('Book: Invalid ISBN.');
- this.isbn = isbn;
- },
- getTitle: function() {
- return this.title;
- },
- setTitle: function(title) {
- this.title = title || 'No title specified';
- },
- getAuthor: function() {
- return this.author;
- },
- setAuthor: function(author) {
- this.author = author || 'No author specified';
- },
- display: function() {
- alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author);
- }
- };
- var theHobbit = new Book('0-395-07122-4', '', 'J. R. R. Tolkein');
- theHobbit.display(); // Outputs the data by creating and populating an HTML element.
- </script>
接口实现,参考接口,定义了好多方法。
内部方法命名加_,例如这个检测的方法 _checkIsbn
- <script type="text/javascript">
- /* 出版 interface. */
- /* var Publication = new Interface('Publication', ['getIsbn', 'setIsbn', 'getTitle',
- 'setTitle', 'getAuthor', 'setAuthor', 'display']); */
- var Book = function(isbn, title, author) { // implements Publication
- this.setIsbn(isbn);
- this.setTitle(title);
- this.setAuthor(author);
- }
- Book.prototype = {
- _checkIsbn: function(isbn) {
- if(isbn == undefined || typeof isbn != 'string') {
- return false;
- }
- return true; // All tests passed.
- },
- getIsbn: function() {
- return this.isbn;
- },
- setIsbn: function(isbn) {
- if(!this._checkIsbn(isbn)) throw new Error('Book: Invalid ISBN.');
- this.isbn = isbn;
- },
- getTitle: function() {
- return this.title;
- },
- setTitle: function(title) {
- this.title = title || 'No title specified';
- },
- getAuthor: function() {
- return this.author;
- },
- setAuthor: function(author) {
- this.author = author || 'No author specified';
- },
- display: function() {
- alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author);
- }
- };
- //var theHobbit = new Book(123, '', 'J. R. R. Tolkein'); // 非字符串抛出异常
- var theHobbit = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein');
- theHobbit.display(); // Outputs the data by creating and populating an HTML element.
- </script>
希望本文所述对大家 JavaScript 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0519/330158.html