انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Javascript String Object with HTML Warpper Methods

الكلية كلية تكنولوجيا المعلومات     القسم قسم شبكات المعلومات     المرحلة 3
أستاذ المادة حۡــسۜــنۨ ا̍ڷــڔهــٻۧــمۘــې       3/6/2012 10:09:23 PM

Please Notice That E-Learning System in Babylon University Removes Comas on submitting refer the Attached File for Full Understanding
Hassan H. Alrehamy - Lect. Asst.


String Object in Javascript

Strings are simply groups of characters, like JavaScript , Hello world! , http://www.quirk smode.org or even 14 . When you write JavaScripts, you need to know what strings are and how they work. You ll use them a lot, since most things you can read out (the URL of a page, a style sheet declaration, the value of a form field) are strings.


When you declare and manipulate strings, always write them with single quotes or double quotes " around them. This tells the browser that it s dealing with a string. Don t mix up your quotes, if you start a string with a single quote and end it with a double quote, JavaScript doesn t understand what you mean. As a rule, I use single quotes because I ve decided to use double quotes for HTML and single quotes for JavaScript.
Let s introduce our two test strings that we ll use throughout this page:
var a = Hello world! ; var b = I am a JavaScript hacker.


Now we have declared two variables, a and b and put strings in them. Having done this, we can start working with them. But first of all, a problem: suppose I d written
var b = I m a JavaScript hacker. ;


This string has a single quote in it, so JavaScript thinks that the string ends, doesn t understand what comes next and gives error messages. So you need to escape the single quote, telling the browser to treat the quote as a character, and not as a command to end the string. This is done by placing a backslash \ before it:

var b = I\ m a JavaScript hacker.

Note that you can put double quotes in the string without escaping them. After all, you ve defined the single quotes as the beginning and end of the string, so

var b = I\ m a JavaScript "hacker".

gives no problems. The double quotes are automatically treated as parts of the string, not as commands. If you want to change a string to a number, first make sure there are only the characters 0-9 in the string.

var c = 1234 ; d = c * 1;

Since multiplying assumes numbers, JavaScript makes the string a number, if possible. If that isn t possible, the result is NaN. Note that you cannot do

var c = 1234 ; d = c + 0;

This would yield 12340 because JavaScript thinks the + means concatenate string, not add.to solve this problem you may need to use ParseInt() or ParseFloat() and to convert back to string you can do ToString()


Manipulating the Object

The String object is used to manipulate a stored piece of text. String objects are created with new String().
Syntax : var txt = new String("string"); // or more simply: var txt = "string";


Examples of use: The following example uses the length property of the String object to find the length of a string:

var txt="Hello world!";
document.write(txt.length); //The code above will result in the following output: 12

as we learnt before in OOPS , each object has a set of properties and a set of methods and as String is object , then it has the following :


Properties :
• Constructor: Returns the function that created the String object s prototype
• length : Returns the length of a string
• prototype: Allows you to add properties and methods to an object


Example of using Prototype : Use the prototype property to add a property to an object:


function employee(name,jobtitle,born) {
this.name=name;
this.jobtitle=jobtitle;
this.born=born; }
function TestEmployee() {
var fred=new employee("Fred Flintstone","Caveman",1970);
employee.prototype.salary=null;
fred.salary=20000;
document.write(fred.salary);
test

المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .