port

The core class of the ports.js.

source ports.js
requires ports.jsonp
version 1.01
updated 2007/09/05
author inamorix <inamorix@metatype.jp>
copyright Copyright (c) 2007, metatype.
license The MIT-style license.

Objects

object port

The core class of the ports.js.

properties

type name description
number version The version of the ports.js.
object libs The list of JavaScript libraries on the ports.js.
object loaded The list of already loaded files on the ports.js.
object installed The list of already installed libraries on the ports.js.
array standby The list of functions of standby.
Executes that when every DOM is accessible.
string base The directory of the ports.js.
string base_f The directory of the ports.js on "fetch".
string query The query to the ports.js.

methods

Functions

port.myself(el)

Returns the URL of a JavaScript file.

examples

port.myself();

parameters

type name description
element el (optional) The element to search for a script tag.
Defaults to 'document'.

returns

type description
string The URL of a JavaScript file.

port.load(file)

Loads a file.

examples

port.load('example.js');

parameters

type name description
string file The URL of file.
File extension reads "css" as a CSS file.

port.install(keys, mode)

Installs a JavaScript library.
The automatically load dependency files on based libraries.

examples

// String (single)
<script>port.install('prototype');</script>
<script>alert($);</script>

// Array (multiple)
<script>port.install(['xxx', 'yyy', 'zzz']);</script>
// And the ports.js supports the install from query.
// The query of the install is "i".

// From query (single)
<script src="ports.js?i=prototype"></script>
<script>alert($);</script>

// From query (multiple)
<script src="ports.js?i=xxx,yyy,zzz"></script>

parameters

type name description
string, array keys The keyword of library.
Array is more keywords.
See the keywords of JavaScript library.
string mode (optional) The mode for make and fetch.
Defaults to 'null'.

port.make(keys)

This is work same as install.
To be different is to use a lightweight compressed file.

examples

<script>port.make('prototype');</script>
// The query of the make is "m".
<script src="ports.js?m=prototype"></script>

parameters

type name description
string, array keys See install.

port.fetch(keys)

This is work same as install.
To be different is to use a online file with download.
When you use only fetch, the ports.js works only in "ports.js" and "ports.jsonp".

examples

<script>port.fetch('prototype');</script>
// The query of the fetch is "f".
<script src="ports.js?f=prototype"></script>

parameters

type name description
string, array keys See install.

port.extend(obj)

Extends the ports.js.

examples

port.extend({
  init: function () {
    this.msg = 'hello';
    return this;
  },
  standby: function () {
    document.getElementById('x').innerHTML = this.msg;
  }
});

parameters

type name description
object obj The list of object of new members.
Some keyword works special.

keywords description
init The function of initialize.
That must return 'this'.
standby The function of standby.
Executes that when every DOM is accessible.

port.isset(val)

Returns 'true' if the variable is defined.

examples

var x;
var y = false;
var z = null;
port.isset(x); // false
port.isset(y); // true
port.isset(z); // true

parameters

type name description
mixed val The variable.

returns

type description
boolean The result of if the variable is defined.

port.isown(obj, key)

Returns 'true' if the property of object is own property.

examples

Object.prototype.x = 'X';
var obj = { y: 'Y', z: 'Z' };
for (i in obj) {
  port.isown(obj, i); // x: false, y: true, z: true
}

parameters

type name description
array, object obj The array or object.
number, string key The key of property.

returns

type description
boolean The result of the property of object is own property.

port.any()

Returns the found first argument in arguments if that is defined.

examples

function fn (el) {
  return port.any(el, document);
}
fn(); // document

parameters

type name description
mixed ... (optional) Any number of variable.

returns

type description
mixed, null The found first argument.
Returns 'null' when there is not it.

port.bind(fn, scope)

Binds a scope of 'this' at inside block of function.

examples

var obj = {
  fn: function (msg) {
    return msg;
  }
};
document.getElementById('x').onclick = port.bind(function (el) {
  el.innerHTML = this.fn('hello');
}, obj);

parameters

type name description
function fn The function.
The first argument is originally 'this'.
object scope (optional) The object of scope of 'this'.
Defaults to 'port'.

returns

type description
function The bound function.

port.each(obj, fn, scope)

Iterates through an array or object.

examples

var obj = {
  x: { a: 1, b: 2, c: 3 },
  fn: function (key, val) {
    return key + ': ' + (n * 100);
  }
};
port.each(obj.x, function (val) {
  alert(val); // 1, 2, 3
});
port.each(obj.x, function (key, val) {
  alert(this.fn(key, val)); // 'a: 100', 'b: 200', 'c: 300'
}, obj);

parameters

type name description
array, object obj The array or object.
function fn The function to execute on iteration.
object scope (optional) The object of scope of 'this' on iteration.
Defaults to 'port'.